数组
var a = [1,2,3];
var b = [1,2,3];
a == b; //false
a == '1,2,3'; //true;
//
var c = [];
Boolean(c); //true
c == false; //true
c == 0; //true
c == ''; //true
c == undefined; //false
var arr = Array.prototype.slice.call( arguments );
//es6
var arr = Array.from( arguments );
var a = "foo";
var c = Array.prototype.join.call( a, "-" );
var d = Array.prototype.map.call( a, function(v){
return v.toUpperCase() + ".";
} ).join( "" );
c; // "f-o-o"
d; // "F.O.O."
//
var c = Array.prototype.reverse.call( a ); //error
var c = a.split( "" ).reverse().join( "" );
var a = [ undefined, undefined, undefined ]; //[ undefined, undefined, undefined ]
var b = new Array( 3 ); //[ undefined x 3 ]
var c = [];
c.length = 3; //[ undefined x 3 ]
var d = [ , , , ]; //[ undefined x 3 ]
a.join( "-" ); // "--"
b.join( "-" ); // "--"
a.map(function(v,i){ return i; }); // [ 0, 1, 2 ]
b.map(function(v,i){ return i; }); // [ undefined x 3 ]
//确实想要表示多个空值而不是不存在;
var a = Array.apply( null, { length: 4 } );
a; // [ undefined, undefined, undefined ,undefined]
数值
0.1 + 0.2 === 0.3; // false
//
if (!Number.EPSILON) { //es6
Number.EPSILON = Math.pow(2,-52);
}
function numbersCloseEnoughToEqual(n1,n2) {
return Math.abs( n1 - n2 ) < Number.EPSILON;
}
var a = 0.1 + 0.2;
var b = 0.3;
numbersCloseEnoughToEqual( a, b ); // true
numbersCloseEnoughToEqual( 0.0000001, 0.0000002 ); // false
function isNegZero(n) {
n = Number( n );
return (n === 0) && (1 / n === -Infinity);
}
- 数值化(Number)
- 对于原始类型:
false//0, true//1, null//0, 数值字符串//数值, 其他//NaN
;
- 对于其他类型: 调用其
valueOf, toString
,对返回的原始类型进行操作;否则报错;
Number( "" ); // 0
Number( [] ); // 0
- 小心使用
parseInt
, 它也会调用内部的toString
方法;
parseInt( 1/0, 19 ); =>parseInt( "Infinity", 19 ) => parseInt( "I", 19 ) // 18;
parseInt( 0.000008 ); // 0 ("0" from "0.000008")
parseInt( 0.0000008 ); // 8 ("8" from "8e-7")
parseInt( false, 16 ); // 250 ("fa" from "false")
parseInt( parseInt, 16 ); // 15 ("f" from "function..")
parseInt( "0x10" ); // 16
parseInt( "103", 2 ); // 2
对象
//引用被切断
function foo(x) {
x.push( 4 );
x; // [1,2,3,4]
// later
x = [4,5,6]; //改变arg[x]的引用
x.push( 7 );
x; // [4,5,6,7]
}
var a = [1,2,3];
foo( a );
a; // [1,2,3,4] not [4,5,6,7]
//引用一直保存
function foo(x) {
x.push( 4 );
x; // [1,2,3,4]
// later
x.length = 0; // empty existing array in-place
x.push( 4, 5, 6, 7 );
x; // [4,5,6,7]
}
var a = [1,2,3];
foo( a );
a; // [4,5,6,7] not [1,2,3,4]
//注意特殊对象的隐式转化
function foo(x) {
x = x + 1;
x; // 3
}
var a = 2;
var b = new Number( a ); // or equivalently `Object(a)`
foo( b );
console.log( b ); // 2, not 3
void操作符
//可能用到的情况
function doSomething() {
// note: `APP.ready` is provided by our application
if (!APP.ready) {
// try again later
return void setTimeout( doSomething, 100 );
}
var result;
// do some other stuff
return result;
}
等值判断
//es6
Object.is( a,b );
不用于严格意义的对比,而是用于特殊例子的比较
var a = 2 / "foo";
var b = -3 * 0;
Object.is( a, NaN ); // true
Object.is( b, -0 ); // true
Object.is( b, 0 ); // false
预定义数值
if(VALUE) {
console.log('value');
} //error
if(typeof VALUE !== "undefined") {
console.log('value')
}
//
function () {
var newValue = (typeof oldValue !== "undefined") ? oldValue : 'newvalue';
...
}
function (oldValue) {
var newValue = oldValue || 'newvalue';
...
}
js原生类型
- String()
- Number()
- Boolean()
- Array()
- Object()
- Function()
- RegExp()
- Date()
- Error()
- Symbol() -- added in ES6!
由于所有类型的本源都是来自Object(),使用Object.prototype.toString.call方法可以现在类型内部的[[class]];
格式为[object X]
//Object
var a = new Object();
var b = Object();
var c = new Object;
//Array
var a = new Array();
var b = Array();
var c = new Array;
//Date
var a = new Date(); //[object Date];
var b = Date(); //string
var c = new Date; //[object Date];
- 各个类型的原型都是其相对应类型的空值;//注意不要污染它们;
Array.prototype; //[];
......