Why some built-in functions in IE8 are not instanc

2019-04-14 04:30发布

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

1条回答
来,给爷笑一个
2楼-- · 2019-04-14 04:56

Found here: http://perfectionkills.com/whats-wrong-with-extending-the-dom/

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.

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.


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.

Examples:

alert;
scrollTo;
document.getElementById;
location.reload;
setTimeout;
查看更多
登录 后发表回答