在阅读 MDN here 时,我遇到了
Starting in JavaScript 1.8.5 toString() called on null returns [object Null], and undefined returns [object Undefined], as defined in the 5th Edition of ECMAScript and a subsequent Errata. See Using toString to detect object type.
我尝试了Object(null).toString()
& Object(undefined).toString()
并且都返回“[object Object]”
根据 specification ,JS 中的原始类型有 Undefined、Null、Boolean、Number 或 String。
那么,目前所有浏览器尚未实现的假设是否正确:
[对象未定义]
& [对象空]
?
最佳答案
要获取内部[[Class]],需要将该值设置为Object.prototype.toString()
的this
值,因此:
Object.prototype.toString.call(null); // [object Null]
Object.prototype.toString.call(undefined); // [object Undefined]
ECMAScript 5 中的定义如下:
8.6.2 Object Internal Properties and Methods
The value of the
[[Class]]
internal property is defined by this specification for every kind of built-in object. The value of the[[Class]]
internal property of a host object may be any String value except one of"Arguments", "Array", "Boolean", "Date", "Error", "Function", "JSON", "Math", "Number", "Object", "RegExp", and "String"
. The value of a [[Class]] internal property is used internally to distinguish different kinds of objects. Note that this specification does not provide any means for a program to access that value except throughObject.prototype.toString
(see 15.2.4.2).
关于javascript - [object Null] 和 [object Undefined] 尚未实现吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16864341/