Closed Bug 339884 Opened 20 years ago Closed 18 years ago

JavaScript "instanceof" incompatible with sharing objects between windows.

Categories

(Core :: JavaScript Engine, defect)

defect
Not set
normal

Tracking

()

RESOLVED WONTFIX

People

(Reporter: jesse, Unassigned)

References

Details

User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.3) Gecko/20060426 Firefox/1.5.0.3 Build Identifier: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.3) Gecko/20060426 Firefox/1.5.0.3 Firefox does not seem to share the global JavaScript objects between windows. I.E. for windows w1 and w2, w1.Object != w2.Object. Therefore, consider the following code, which works fine in IE: var w = window.open("", "newWindow"); w.document.open(); w.document.write("<html><body><div>Hello World!</div></body></html>"); w.document.close(); w.document.childNodes instanceof Object; w.document.addEventListener instanceof Function; The last two lines are false in Firefox since Object and Function are shorthand for window.Object and window.Function. The w.document.childNodes object is actually an instance of w.Object. I'm not sure whether there is a relevant spec which would reveal whether this is a bug or a feature. At the least it seems like an inconvenience. At the very least the owning window can be accessed from any HTMLElement by elm.ownerDocument.defaultView. So "elm.aFunct instanceof elm.ownerDocument.defaultView.Function" is a possible work around. However, this is the only type of object that can be handled in this way. Instances of Object, Function, Array, String have no path to the owning window. Reproducible: Always Steps to Reproduce: See JavaScript in Details. Actual Results: w.document.childNodes instanceof Object == false Expected Results: w.document.childNodes instanceof Object == true
Blocks: tibco
Assignee: nobody → general
Component: General → JavaScript Engine
Product: Firefox → Core
QA Contact: general → general
Version: unspecified → Trunk
There are no "global JavaScript objects" to share between windows -- windows are each a global object for the scripts loaded in the window, and each global window object has its own constructors, with their own prototypes. IE is doing something that is not compatible with ECMA-262 extended to work with multiple global window objects. I think this bug should be marked invalid, but I agree it's a problem that you can't easily classify objects created in other windows. For ECMA-262 Edition 4 / JS2, we are providing an "is" operator that tests membership of a "Platonic type", not whether the object's prototype chain ends in a particular window's constructor's prototype object. So relief is in sight. I'd like to hear what Opera and Safari do, and consider how widespread scripts that depend on IE's behavior here might be. /be
FWIW, ChatZilla works around this by using its own "isinstance" function in place of any and every instanceof operation, defined thus: http://bonsai.mozilla.org/cvsblame.cgi?file=mozilla/extensions/irc/js/lib/utils.js&rev=1.65&mark=1204-1214#1203 Not pretty, and probably a bit dodgy, but it suffices for our needs in today's world.
JS2/ES4 (http://ecmascript.org/) will have an "is" operator that works across windows, using the underlying universal type Object, not the mutable, per-window constructor function Object, e.g. instanceof is well-defined by ECMA-262 Edition 3 and it's not going to change. It is a pain in browsers if you are operating across windows, but ECMA 3rd edition folks didn't think about that use-case, AFAIK. /be
Status: UNCONFIRMED → RESOLVED
Closed: 18 years ago
Resolution: --- → WONTFIX
ES5 adds Array.isArray. If you can't find it on Array, you're running in an older implementation, where you could try other hacks to detect arrayness. /be
You need to log in before you can comment on or make changes to this bug.