javascript IE 中的无效调用对象错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19321938/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Invalid Calling Object error in IE
提问by Jaboc83
So I'm getting the error 'Invalid Calling Object' in IE11 when trying to execute the following:
因此,当我尝试执行以下操作时,在 IE11 中收到错误“无效的调用对象”:
window.toString.call({});
When I expect to see => "[object Object]"
当我希望看到 => "[object Object]"
This form seems to works though:
不过,这种形式似乎有效:
({}).toString();
Both forms appear to work fine in chrome, am I missing something?
这两种形式在 chrome 中似乎都可以正常工作,我错过了什么吗?
回答by Paul S.
You seem to be neglecting the fact
你似乎忽略了这个事实
window.toString === Object.prototype.toString; // false
The Window'stoString
is implementation-specific and there is nothing in the specification saying methods on DOM Host Objectshave to work with call
/on other objects/etc
该窗口的toString
是实现特定的,没有什么规范话说方法DOM主机对象都将工作与call
/其他物体上的/ etc
If you want to capture this toString
but can't assume prototype, try
如果你想捕捉这个toString
但不能假设原型,试试
var toString = ({}).toString;
toString.call({}); // "[object Object]"
You could also consider skipping call
each time by wrapping it or using bind
您也可以考虑call
通过包装或使用每次跳过bind
var toString = function (x) { return ({}).toString.call(x); };
toString(10); // "[object Number]"
// or
var toString = ({}).toString.call.bind(({}).toString);
toString(true); // "[object Boolean]"