为什么`Object.prototype.toString`总是返回`[object *]`? [英] Why does `Object.prototype.toString` always return `[object *]`?

查看:177
本文介绍了为什么`Object.prototype.toString`总是返回`[object *]`?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果调用 Object.prototype.toString.call(任何东西),则结果始终为 [object Something] ,其中某事可能是几件事之一。我的问题是为什么对象部分在那里?始终将其保留在那里似乎是多余的。

If you call Object.prototype.toString.call(anything) the result is always [object Something], where Something could be one of several things. My question is why is the "object" part there? It seems superfluous to always have it there. It doesn't tell you anything about the argument which was passed in.

Object.prototype.toString.call(null);
=> [object Null]

Object.prototype.toString.call(undefined);
=> [object Undefined]

由于 null 未定义不是对象(并且 CheckObjectCoercible 失败),对象部分确实确实没有意义。

Since null and undefined aren't objects (and fails CheckObjectCoercible), the "object" part really, really seems meaningless.

我不得不认为最初有一些原因将对象部分放在这里,即使此后原因已经丢失,现在也只是出于历史原因保留了下来。原因。

I have to think there was some reason originally that the "object" part was put there, even if the reason has been lost in the time since, and it's now just preserved for historical reasons.

有人可以帮忙阐明一下吗?

Can anyone help shed some light on this?

要清楚,我已经知道如何使用 Object.prototype.toString 来获取 [[Class]] (类型)对象。我的问题与返回的字符串的格式有关,特别是开头的对象部分。我想知道这部分是否有原因,或者将来可能有这个原因。也许将来它会返回对象以外的东西?我可以期望它总是返回对象吗?如果是这样,为什么还要返回它呢?谁想要一个函数,不管输入是什么,总是返回相同的东西?

To be clear, I already know how Object.prototype.toString can be used to get the [[Class]] (type) of an object. My question concerns the reason for the format of the returned string -- specifically the "object" part at the beginning. I want to know if there was ever a reason for this part or if there is a possible future reason for this part. Maybe in the future it could return something other than "object"? Can I expect it to always return "object"? If so, why does it even return it? Who wants a function to always return the same thing no matter what the input is?

推荐答案

ES5.1规范的附件F 表示关于 Object.prototype.toString


15.2.4.2:版本5对undefined和null的处理,因为此值导致现有代码失败。修改规范以保持与此类代码的兼容性。新的步骤1和2已添加到算法中。

15.2.4.2: Edition 5 handling of undefined and null as this value caused existing code to fail. Specification modified to maintain compatibility with such code. New steps 1 and 2 added to the algorithm.

这是ES5.1规范中所做的更正。在ES5之前,将null或undefined传递给toString总是导致改为传递全局对象。 ES5中与模式严格相关的更改会导致传递null和undefined而不进行修改。如ES5中所指定,将null或undefined传递给 Object.prototype.toString 会导致TypeError异常。该异常破坏了一些现有代码,因此我们不得不修复该规范以免发生这种情况。

This is a correction that was made in the ES5.1 spec. Prior to ES5, passing null or undefined to toString always caused the global object to be passed instead. Strict mode related changes in ES5 cause null and undefined to be passed without modification. As specified in ES5, passing null or undefined to Object.prototype.toString caused a TypeError exception. This exception broke some existing code so we had to fix the spec to not throw in that case.

那么 toString 返回null和未定义?事实证明,许多现有代码还期望 Object.prototype.toString 始终返回形式为 [object *] 。因此,我们决定使null和undefined产生 [object Null] [object Undefined]

So what should toString return for null and undefined? It turns out that a lot of existing code also expects Object.prototype.toString to always return a string of the form "[object *]". So, we decided to make null and undefined produce "[object Null]" and "[object Undefined]".

这似乎已经奏效,并且通常允许现有的基于ES3的代码在与ES5 / 5.1中的严格模式相关的更改下继续工作。

This seems to have worked and generally permitted existing ES3-based code to keep working in the presence of the strict mode related changes in ES5/5.1.

这篇关于为什么`Object.prototype.toString`总是返回`[object *]`?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆