为什么会发生这种情况?
基本上,因为 IE 讨厌 Web 开发人员并且正在扰乱你。
More seriously, things provided by the browser implementation that are not part of the core Javascript language may be classified as host objects http://github.com/#x4.3.8. When it comes to host objects all bets are off and they are basically allowed to do anything they want[1] without needing to respect usual Javascript semantics.
获得实际 setTimeout 的解决方法是什么?
我知道它真的很难看,但是您可以执行 if-else-if 链直至预定义数量的参数。在 setTimeout 的情况下,这不应该是一个大问题,因为您不需要超过 2 或 3 个参数。
var _oldSetTimeout = window.setTimeout;
window.setTimeout = function (a1, a2, a3)
{
switch(arguments.length){
case 0: return _oldSetTimeout();
case 1: return _oldSetTimeout(a1);
case 2: return _oldSetTimeout(a1, a2);
default: return _oldSetTimeout(a1, a2, a3);
}
};
虽然这是一个非常丑陋的解决方案,但有时这是唯一的方法。例如,也无法使用可变参数调用构造函数。
[1] 为了让您了解主机对象有多么邪恶,前几天我必须对 DOM 节点/文档中的 XPath 方法进行特征检测。而不是通常的if(node.selectNodes)
我必须使用的测试if("selectNodes" in node)
因为节点是 IE 中的主机对象,只需访问 selectNodes 属性实际上就会调用它,从而给我一个“参数数量不正确”的异常!