为什么IE8中的一些内置函数不是Function的实例

Why some built-in functions in IE8 are not instances of Function?

本文关键字:Function 实例 函数 IE8 为什么 内置      更新时间:2023-09-26

我注意到alert和console.log在IE8中不像正常的JavaScript对象那样工作。有人对此有什么解释吗?

Good:
escape instanceof Function; // => true
escape.call;                // => function call() { }
typeof escape;              // => "function"
escape.test = 1;            // => 1
Bad:
alert instanceof Function;  // => false
alert.call;                 // => undefined
typeof alert;               // => "object"
alert.constructor;          // => undefined
alert.test = 1;             // => Object doesn't support this property or method

在此找到:http://perfectionkills.com/whats-wrong-with-extending-the-dom/

ecma - 262 3。艾德:

宿主对象可以用any实现这些内部方法依赖于实现的行为,也可能是宿主对象只实现部分内部方法,不实现其他方法。

内部方法规范讨论的是[[Get]]、[[Put]]、[[Delete]]等。注意它是如何说内部方法的行为是依赖于实现的。这意味着宿主对象在调用[[Get]]方法时抛出错误是绝对正常的。


所以,IE并没有违反规范,行为是一致的,所有不是JavaScript语言一部分的内置函数都是这样工作的。你不能给它们分配属性,它们没有原型和构造函数。

例子:

alert;
scrollTo;
document.getElementById;
location.reload;
setTimeout;