IE 中的调用对象无效错误

Invalid Calling Object error in IE

本文关键字:无效 错误 对象 调用 IE      更新时间:2023-09-26

所以我在尝试执行以下内容时在IE11中收到错误"无效的调用对象":

window.toString.call({});

当我期望看到 => "[对象对象]"

不过,这种形式似乎有效:

({}).toString();

两种形式在铬中似乎都可以正常工作,我错过了什么吗?

你似乎忽略了这个事实

window.toString === Object.prototype.toString; // false

窗口的toString是特定于实现的,规范中没有任何内容说 DOM 主机对象上的方法必须与call/在其他对象上/等一起使用

如果您想捕获此toString但无法假设原型,请尝试

var toString = ({}).toString;
toString.call({}); // "[object Object]"

您也可以考虑每次通过包装或使用bind来跳过call

var toString = function (x) { return ({}).toString.call(x); };
toString(10); // "[object Number]"
// or
var toString = ({}).toString.call.bind(({}).toString);
toString(true); // "[object Boolean]"