javascript - [object Null] 和 [object Undefined] 尚未实现吗?

标签 javascript null undefined specifications ecmascript-5

在阅读 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 through Object.prototype.toString (see 15.2.4.2).

关于javascript - [object Null] 和 [object Undefined] 尚未实现吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16864341/

相关文章:

c++ - 未初始化的本地字符的行为?

javascript - 检查对象及其方法是否都已定义的单行代码

excel - 用上面单元格中的值替换 powerquery 列中的空值

javascript - 如何通过从数据库中获取像素值来为图像着色

javascript - Firefox 插件 : Load URI Event

javascript - slider 导航按钮将不显示

sql - 替换 LEFT OUTER JOIN 中返回的空值

performance - 在 postgresql 的列上设置 "NOT NULL"会提高性能吗?

c - 错误未定义对 'WinMain@16' 的引用

javascript - 在 React JS 的图标上方有文字