var a = ‘0’;
var b = a ? ‘me’:’hi’;
console.log(b);
//false 有: undefined , 0, ”, false,null
//true 有:’0′,[],{},’ ‘
var a = ‘ ‘;
var b = a || ‘为假的’;
console.log(b);
//为假的,有: undefined , 0, ”, false,null
//原样输出的 有:’0′,[],{},’ ‘
var a = {};
var b = a && ‘为真的’;
console.log(b);
//原样输出的,有: undefined , 0, ”, false,null
//为真的, 有:’0′,[],{},’ ‘
以上三个的测试结果都说明了:undefined , 0 ,”,false,null 为假; ‘0’,’ ‘,[],{}等为真。
js类型测试:
var arr = [‘dd’,’fdf’];
var obj = {};
console.log(Object.prototype.toString.apply(arr));
console.log(Object.prototype.toString.apply(obj));
var s= ”;
console.log(Object.prototype.toString.apply(0));
console.log(Object.prototype.toString.apply(s));
console.log(Object.prototype.toString.apply(false));
console.log(Object.prototype.toString.apply(undefined));
输出为:
[object Array]
[object Object]
[object Number]
[object String]
[object Boolean]
[object Undefined]
最新评论