最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Why "foo".toString() is not the same as toString.call("foo")? - Stack Overflow

programmeradmin6浏览0评论

Here is a question in JavaScript below:

// 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() ?

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?

toString in String[not found] --> toString in String.prototype[not found]

                           --> toString in Object.prototype[found]

Here is a question in JavaScript below:

// 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() ?

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?

toString in String[not found] --> toString in String.prototype[not found]

                           --> toString in Object.prototype[found]
Share Improve this question edited Jun 20, 2020 at 9:12 CommunityBot 11 silver badge asked Jan 1, 2012 at 11:52 pat.insidepat.inside 1,8344 gold badges18 silver badges25 bronze badges 4
  • Not a javascript expert, but I would suspect it has to do with calling a predefined function and passing it a null parameter as opposed to calling a function on a nonexistent object. – user684934 Commented Jan 1, 2012 at 11:55
  • How do you inspect those results? Browser console, or something else? – Shadow Wizard Commented Jan 1, 2012 at 11:55
  • @ShadowWizard Chrome Browser console. – pat.inside Commented Jan 1, 2012 at 12:02
  • I thought it was just bad display of the browser, but looks like I was wrong, sorry. – Shadow Wizard Commented Jan 1, 2012 at 12:11
Add a ment  | 

3 Answers 3

Reset to default 17

String.prototype.toString overrides Object.prototype.toString. They are not the same function.

From the specification of 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.)

And Object.prototype.toString:

When the toString method is called, the following steps are taken:

  1. Let O be the result of calling ToObject passing the this value as the argument.
  2. Let class be the value of the [[Class]] internal property of O.
  3. Return the String value that is the result of concatenating the three Strings "[object ", class, and "]".

Arrays behave similar, they also override toString():

> [1,2].toString()
  "1,2"
>>> String.prototype.toString.call("foo")
"foo"

Object is not the same thing as a String.

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.

发布评论

评论列表(0)

  1. 暂无评论