window.toString.call is undefined in IE8

两盒软妹~` 提交于 2019-12-01 17:15:48

window is a host object, and the ECMAScript Language Specification (3rd edition) does not require host objects to be derived from the native Object object. In IE (and probably in some other browsers) host objects aren't, so they don't support any of the native methods or properties (although they may have methods or properties with the same names as native methods or properties which are accessible to scripts).

If all you want is to get hold of the language implementation's default native toString method then you should use Object.prototype.toString.call("").

NickFitz is correct, the toString method on the host object that you are finding is purely so that if you did

alert(window);

you would get the text [object]

All that the javascript method toString() used in your examples would acheive is to make a string from a string so the correct way to do what you are trying is;

var a =new String ("");

or simply

var b = "";

or if you really want to be silly;

var b = "".toString();
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!