[10] === 10 // is false
[10] == 10 // is true
‘10‘ == 10 // is true
‘10‘ === 10 // is false
[] == 0 // is true
[] === 0 // is false
‘‘ == false // is true but true == "a" is false
‘‘ === false // is false
3、使用对象构造器
function Person(firstName, lastName){
this.firstName = firstName;
this.lastName = lastName;
}
var Saad = new Person("Saad", "Mousliki");
var myArray = [12 , 222 , 1000 , 124 , 98 , 10 ];
myArray.length = 4; // myArray will be equal to [12 , 222 , 1000 , 124].
与此同时,如果把length属性变大,数组的长度值变会增加,会使用undefined来作为新的元素填充。length是一个可写的属性。
myArray.length = 10; // the new array length is 10
myArray[myArray.length - 1] ; // undefined
14、在条件中使用逻辑与或
var foo = 10;
foo == 10 && doSomething(); // is the same thing as if (foo == 10) doSomething();
foo == 5 || doSomething(); // is the same thing as if (foo != 5) doSomething();
逻辑或还可用来设置默认值,比如函数参数的默认值。
function doSomething(arg1){
arg1 = arg1 || 10; // arg1 will have 10 as a default value if it’s not already set
}
15、保留指定小数位数
var num =2.443242342;
num = num.toFixed(4); // num will be equal to 2.4432
注意,toFixec()返回的是字符串,不是数字。
16、浮点计算的问题
0.1 + 0.2 === 0.3 // is false
9007199254740992 + 1 // is equal to 9007199254740992
9007199254740992 + 2 // is equal to 9007199254740994
为什么呢?因为0.1+0.2等于0.30000000000000004。JavaScript的数字都遵循IEEE 754标准构建,在内部都是64位浮点小数表示,具体可以参见JavaScript中的数字是如何编码的.