js中boolean类型的相关比较

来源:互联网 发布:石家庄软件培训学校 编辑:程序博客网 时间:2024/06/06 01:42

console.log(([])?true:false); //true
console.log(([]==false?true:false)); //true
console.log(({}==false)?true:false) //false

原因:
布尔类型与其它任何类型进行比较,布尔类型将会转换为number类型。
Number([])返回0所以第二题为true

Number转换类型的参数如果为对象返回的就是NaN,
那么Number({})返回的就是NaN。
通过Object.prototype.toString.call({})来判断类型,0与NaN相比为false

原创粉丝点击