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]
String.prototype.toStringoverridesObject.prototype.toString. They are not the same function.From the specification of
String.prototype.toString:And
Object.prototype.toString:Arrays behave similar, they also override
toString():