为什么IE8中的某些内置函数不是Function的实例? [英] Why some built-in functions in IE8 are not instances of Function?

查看:96
本文介绍了为什么IE8中的某些内置函数不是Function的实例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到alert和console.log不像IE8中的普通JavaScript对象那样工作。有没有人对此有解释?

I noticed that alert and console.log do not work like normal JavaScript objects in IE8. Does anyone have explanation for this?

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名。 ed:

ECMA-262 3rd. ed:


主机对象可以使用任何
依赖于实现的行为来实现这些内部方法,或者它可能是宿主对象
只实现了一些内部方法而不是其他方法。

Host objects may implement these internal methods with any implementation-dependent behaviour, or it may be that a host object implements only some internal methods and not others.

内部方法规范谈到的是[[Get]],[ [Put]],[[Delete]]等。请注意它是如何表示内部方法行为依赖于实现。这意味着主机对象在调用[[Get]]方法时抛出错误是绝对正常的。

The internal methods specification talks about are [[Get]], [[Put]], [[Delete]], etc. Note how it says that internal methods behavior is implementation-dependent. What this means is that it's absolutely normal for host object to throw error on invocation of, say, [[Get]] method.

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

So, IE doesn't violate the spec. The behaviour is consistent, and all built-in functions that are not a part of JavaScript language work like that. You cannot assign properties to them, they do not have prototypes and constructors.

示例:

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

这篇关于为什么IE8中的某些内置函数不是Function的实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆