1

When comparing functions from the prototype of a type across frames (same origin), I get the strangest behavior:

>>> window.frames[0].HTMLDocument.prototype.open === HTMLDocument.prototype.open
false

It's exactly the same behavior when using the less-strict ==.

Any idea why this happens?

neuroo
  • 51
  • 1
  • 3
  • For reference: This relates to what is defined in the ES6 spec as "realms": http://people.mozilla.org/~jorendorff/es6-draft.html#sec-code-realms (also related: http://people.mozilla.org/~jorendorff/es6-draft.html#sec-well-known-intrinsic-objects). – Felix Kling Mar 19 '15 at 18:37

2 Answers2

3

Different frames mean different global objects. You will have two distinct objects that do the same. Similarly, window.Object !== frames[0].Object. This is also why you shouldn't use instanceof Array when possibly dealing with multiple environments.

Notice that this has nothing to do with the equality operator you were using, they work the same on objects and compare them by reference.

Community
  • 1
  • 1
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
3

Each browsing context has a separate JavaScript environment.

According to the W3C's HTML5 spec on Web APIs:

Whenever a new Window object is created, it must also create a script settings object...

When the script settings object is created, for each language supported by the user agent, create an appropriate execution environment as defined by the relevant specification.

This requires that every browsing context (i.e., every page in a tab, frame, etc.) have a separate realization of the JavaScript environment. This means that every single page must have a different object for each page (thereby failing a == check) for environmental objects, constructors, and other functions.

Community
  • 1
  • 1
apsillers
  • 112,806
  • 17
  • 235
  • 239