Here is a question in JavaScript below:
下面是JavaScript的一个问题:
// Tested via Google Chrome console.
var toString = Object.prototype.toString;
"foo".toString(); // "foo"
toString.call("foo"); // [object String]
[].toString(); // ""
toString.call([]); // [object Array]
{}.toString(); // syntax error
toString.call({}); // [object Object]
Why the result of toString is different with toString.call() ?
为什么toString的结果与toString.call()不同?
UPDATED
更新
String.prototype.toString.call("foo"); // "foo"
Object.prototype.toString.call("foo"); // [object String]
Is String.prototype.toString not from the prototype chain like below?
String.prototype。不像下面这样从原型链中串出?
toString in String[not found] --> toString in String.prototype[not found]
toString in String[not found]——> toString in String。原型(未找到)
--> toString in Object.prototype[found]
3 个解决方案
#1
16
String.prototype.toString
overrides Object.prototype.toString
. They are not the same function.
String.prototype。toString覆盖Object.prototype.toString。它们不是同一个函数。
From the specification of String.prototype.toString
:
来自于String.prototype.toString的规范:
Returns this String value. (Note that, for a String object, the toString method happens to return the same thing as the valueOf method.)
返回此字符串值。(注意,对于字符串对象,toString方法恰好返回与valueOf方法相同的东西。)
And Object.prototype.toString
:
和Object.prototype.toString:
When the toString method is called, the following steps are taken:
调用toString方法时,采取以下步骤:
- Let O be the result of calling ToObject passing the this value as the argument.
- 让O是调用ToObject的结果,并将此值作为参数传递。
- Let class be the value of the [[Class]] internal property of O.
- 让类是O的[[class]]内部属性的值。
- Return the String value that is the result of concatenating the three Strings "[object ", class, and "]".
- 返回字符串值,该值是连接三个字符串“[object]”、“class”和“]”的结果。
Arrays behave similar, they also override toString()
:
数组的行为类似,它们也覆盖toString():
> [1,2].toString()
"1,2"
#2
4
>>> String.prototype.toString.call("foo")
"foo"
Object is not the same thing as a String.
对象不同于字符串。
#3
-1
The global toString
function is different to the object.toString()
function. According to this source, the global toString
function is not very well defined, and thus badly implemented across different browsers. Essentially it provides functionality similar to the typeof
operator.
全局toString函数与object.toString()函数不同。根据这个源,全局toString函数没有很好的定义,因此在不同的浏览器中执行得很差。本质上,它提供了与类型运算符类似的功能。
#1
16
String.prototype.toString
overrides Object.prototype.toString
. They are not the same function.
String.prototype。toString覆盖Object.prototype.toString。它们不是同一个函数。
From the specification of String.prototype.toString
:
来自于String.prototype.toString的规范:
Returns this String value. (Note that, for a String object, the toString method happens to return the same thing as the valueOf method.)
返回此字符串值。(注意,对于字符串对象,toString方法恰好返回与valueOf方法相同的东西。)
And Object.prototype.toString
:
和Object.prototype.toString:
When the toString method is called, the following steps are taken:
调用toString方法时,采取以下步骤:
- Let O be the result of calling ToObject passing the this value as the argument.
- 让O是调用ToObject的结果,并将此值作为参数传递。
- Let class be the value of the [[Class]] internal property of O.
- 让类是O的[[class]]内部属性的值。
- Return the String value that is the result of concatenating the three Strings "[object ", class, and "]".
- 返回字符串值,该值是连接三个字符串“[object]”、“class”和“]”的结果。
Arrays behave similar, they also override toString()
:
数组的行为类似,它们也覆盖toString():
> [1,2].toString()
"1,2"
#2
4
>>> String.prototype.toString.call("foo")
"foo"
Object is not the same thing as a String.
对象不同于字符串。
#3
-1
The global toString
function is different to the object.toString()
function. According to this source, the global toString
function is not very well defined, and thus badly implemented across different browsers. Essentially it provides functionality similar to the typeof
operator.
全局toString函数与object.toString()函数不同。根据这个源,全局toString函数没有很好的定义,因此在不同的浏览器中执行得很差。本质上,它提供了与类型运算符类似的功能。