首先来看一下两者是怎么对待undefined的:
- alert(typeof undefined);
- alert(Object.prototype.toString.apply(undefined));
结果为:
undefined
[object Object]
由此看出,在Object.prototype.toString.apply眼里undefined也是Object。
接着来看一下是怎么对待null的:
- alert(typeof null);
- alert(Object.prototype.toString.apply(null));
结果为:
object
[object Object]
大家意见一致。typeof说:你可以不穿衣服来,但你一定要来!邪恶啊~
本文通过JavaScript内置函数typeof和Object.prototype.toString.apply方法,探讨了undefined和null两种特殊类型的检测方式及表现形式。对于undefined, typeof返回'undefined', 而Object.prototype.toString.apply返回'[object Object]';对于null, typeof返回'object', Object.prototype.toString.apply同样返回'[object Object]'。

196

被折叠的 条评论
为什么被折叠?



