window.toString.call在IE8中未定义 [英] window.toString.call is undefined in IE8
问题描述
当您运行:
window.toString.call()
/ pre>
在FF / CH中一切都很好,但在IE8中,您会收到脚本错误。更多的调查结果证明,IE8中未定义
window.toString.call
你也可以运行这一个:
window.toString instanceof Function;
// false
alert(window.toString);
//函数toString(){
// [本机代码]
//}
为什么会这样解决呢?我开始想知道jQuery是如何工作的?
解决方案
窗口
是主机对象, ECMAScript语言规范(第3版)不要求主机对象从本地Object
对象。在IE(可能还有一些其他浏览器)中,主机对象不是,所以它们不支持任何本机方法或属性(尽管它们可能具有与本地方法或属性相同的名称的方法或属性,脚本)。
如果你想要的是掌握语言实现的默认本地
toString
方法,那么你应该使用Object.prototype.toString.call()
。When you run:
window.toString.call("")
everything's fine in FF/CH but in IE8 you get a script error. Investigating a bit more it turned out, that
window.toString.call
is undefined in IE8?You can also run this one:
window.toString instanceof Function; // false alert(window.toString); // function toString() { // [native code] // }
Why is that and how to solve it? And I started wondering how come jQuery works in the first place?
解决方案
window
is a host object, and the ECMAScript Language Specification (3rd edition) does not require host objects to be derived from the nativeObject
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 useObject.prototype.toString.call("")
.这篇关于window.toString.call在IE8中未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!