這裏是JavaScript的以下問題:爲什麼「foo」.toString()與toString.call(「foo」)不一樣?
// 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]
爲什麼的toString的結果是不同與toString.call()?
修訂
String.prototype.toString.call("foo"); // "foo"
Object.prototype.toString.call("foo"); // [object String]
是String.prototype.toString不是從原型鏈像下面?
的toString字符串[未找到] - > [未找到]在String.prototype的toString
--> toString in Object.prototype[found]
不是一個JavaScript專家,但我懷疑它與調用預定義的函數,並傳遞一個null參數,而不是做在一個不存在的對象上調用一個函數。 – bdares 2012-01-01 11:55:27
你如何檢查這些結果?瀏覽器控制檯,還是別的? – 2012-01-01 11:55:50
@ShadowWizard Chrome瀏覽器控制檯。 – 2012-01-01 12:02:26