什麼是表達式?
表達式是指能計算出值的任何可用程序單元。
表達式是一種JS短語,可使JS解釋器用來產生一個值。
原始表達式
- 常量、直接量:3.14,“test”
- 關鍵字:null,this,true
- 變量:i,k,j
數組、對象的初始化表達式
- [1,2] 如: new Array(1,2);
- [1,4] 如: [1,undefined,undefined,4]
- {x:1,y:2} 如:var o =new Object(); o.x=1; o.y=2;
函數表達式
var fe =function(){};
(function(){console.log('hello world');})();
屬性訪問表達式
var o ={x:1};
o.x
o['x']
調用表達式
func()
對象創造表達式
new Func(1,2);
new Object();
運算符
按運算符的操作數數量可以分爲:
- 一元:
+num
- 二元:
a+b
- 三元:
c?a:b
按功能可以將運算符分爲:
- 賦值:
x+=1
- 比較:
a==b
- 算術:
a-b
- 位:
a|b
- 邏輯:
exp1 && exp2
- 字符串:
"a"+"b"
- 特殊:
delete obj.x
運算符?:
c ? a : b
var val = true ? 1 : 2 //val=1
運算符,
a , b //從左到右一次運算,逗號運算符不算常見
var val = ( 1 , 2 , 3 ); //val=3
運算符delete
delete obj.x
var obj = {x :1}
obj.x //1
delete obj.x
obj.x //undefined
運算符in
window.x = 1;
'x' in window; //true
運算符instanceof,typeof
{}instanceof Object //true
typeof 100 === 'number' //true
運算符new
function Foo(){}
Foo.prototype.x = 1;
var obj = new Foo();
obj.x; //1
obj.hasOwnProperty('x'); //false
obj._proto_.hasOwnProperty('x'); //true
運算符this
this; //window(瀏覽器)
var obj={
func:function(){return this;}
};
obj.func() // obj
運算符void
void 0 // undefined
void(0) //undefined