javaScript 共有5種基本數(shù)據(jù)類型:Null,Undefined,String,Number,Boolean
1.下面代碼的執(zhí)行結(jié)果:
'''
var arr = [];
arr[0] = 0;
arr[1] = 1;
arr.foo = "v";
alert(arr.length); ?//結(jié)果為2
'''
解析:1.數(shù)組也是對象;2.對象不能用length返回其擁有的屬性數(shù)量
arr.length,對arr對象的length屬性進(jìn)行一個訪問
arr.foo = 'hello' 對arr對象創(chuàng)建一個屬性,所以.foo 跟.length地位是并列的:就是arr的一個屬性,同時(shí)arr的數(shù)組方法跟這些屬性是毫不相關(guān)的
2.
'''
var color ?= 'green';
var test4399 = {
color:'blue',
getColor:function(){
var color ='red';
alert(this.color);
? ?}
}
var getColor = test4399.getColor;//即var getColor = function(){var color = "red";alert(this.color);};
getColor();? ? ? ? ? //執(zhí)行g(shù)etColor()函數(shù)時(shí)this指向的window,因?yàn)閣indow.color為green,所以彈出green
test4399.getColor();//此時(shí)this指向的是test4399,test4399.color為blue,所以彈出blue
'''
結(jié)果:green , blue
getColor();相當(dāng)于普通的函數(shù)調(diào)用,此時(shí)this指向window,this.color應(yīng)該為全局變量的值
test4399.getColor();此時(shí)this指向調(diào)用函數(shù)的對象test4399,因此this.color應(yīng)該為對象的屬性值
知識點(diǎn)一: ?js函數(shù)調(diào)用時(shí)加括號和不加括號的區(qū)別.不加括號相當(dāng)于把函數(shù)代碼賦給等號左邊,加括號是把函數(shù)返回值賦給等號左邊。
知識點(diǎn)二: ?js中this的用法,this總是指向調(diào)用它的對象,倒數(shù)第二行的getColor為windows調(diào)用的,倒數(shù)第一行的getColor是test4399對象調(diào)用的
3.執(zhí)行如下程序:
'''
var x=0;
switch(++x) {
? ? case0: ++x;
? ? case1: ++x;
? ? case2: ++x; ? ? ?//沒有break,結(jié)果為3.
}
'''
4.
var obj = {};
obj.log= console.log;
obj.log.call(console,this);