Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>No, it's not a bug (at least, not in the browser), and it's not just IE.</p> <p>Each document the browser shows has its own global environment, including prototypes for the basic objects. They're each a blank slate. The <code>Array</code> constructor in one document is not the same as the <code>Array</code> constructor in another document, and those contructors don't share a common <code>prototype</code> property; the environments are disconnected. That's why <code>instanceof</code> doesn't work well in multi-document web applications.</p> <p>Consider this example (<a href="http://jsbin.com/urumav/5" rel="nofollow">live copy</a>):</p> <pre><code>window.onload = function() { document.getElementById('theButton').onclick = function() { var wnd, start; display("Adding property to &lt;code&gt;Array.prototype&lt;/code&gt;"); Array.prototype.__foo = "bar"; display("Testing property locally, &lt;code&gt;[].__foo = " + [].__foo + "&lt;/code&gt;"); display("Opening pop-up window"); wnd = window.open("http://jsbin.com/omopaw/3"); display("Waiting for window to load..."); start = new Date().getTime(); checkWindowLoad(); function checkWindowLoad() { var a, b; if (typeof wnd.getArray === "undefined") { if (new Date().getTime() - start &gt; 10000) { display("Error, 10 seconds and the window hasn't loaded"); } else { setTimeout(checkWindowLoad, 10); } return; } display("Window loaded, getting array from it"); a = wnd.getArray(); display("&lt;code&gt;a&lt;/code&gt; contents: " + a.join(", ")); display("&lt;code&gt;a.__foo = " + a.__foo + "&lt;/code&gt;"); display("&lt;code&gt;a instanceof Array&lt;/code&gt;? " + (a instanceof Array)); display("&lt;code&gt;wnd.Array === Array&lt;/code&gt;? " + (wnd.Array === Array)); display("Creating &lt;code&gt;b&lt;/code&gt;"); b = [3, 4, 5]; display("&lt;code&gt;b.__foo = " + b.__foo + "&lt;/code&gt;"); display("Passing &lt;code&gt;b&lt;/code&gt; to the popup, look there for the result."); wnd.callback(b); } }; function display(msg) { var p = document.createElement('p'); p.innerHTML = msg; document.body.appendChild(p); } }; </code></pre> <p>That opens a pop-up window and then retrieves an array from it. But the array we receive was constructed by a <em>different</em> <code>Array</code> object than the one in our window, and so doesn't have the additions we made to <em>our</em> <code>Array.prototype</code>. Then it creates an array instance that <em>does</em> have those additions and passes it to the pop-up, which sees the additions correctly (since the object continues to reference its original prototype):</p> <p>Output in the main window:</p> <blockquote> <p>Adding property to <code>Array.prototype</code> <br>Testing property locally, <code>[].__foo = bar</code> <br>Opening pop-up window <br>Waiting for window to load... <br>Window loaded, getting array from it <br><code>a</code> contents: 1, 2, 3 <br><code>a.__foo = undefined</code> <br><code>a instanceof Array</code>? false <br><code>wnd.Array === Array</code>? false <br>Creating <code>b</code> <br><code>b.__foo = bar</code> <br>Passing <code>b</code> to the popup, look there for the result.</p> </blockquote> <p>...and in the pop-up:</p> <blockquote> <p>I'm the popup. <br>Received <code>x</code>: 3,4,5 <br><code>x.__foo = bar</code></p> </blockquote> <p>You'll see that output both on IE (I specifically tested IE9, but it works on IE7 and I'm sure others as well) and on other browsers.</p> <p>The environments in the two different windows can talk to each other, but they don't have shared globals; the <code>Array.prototype</code> in the parent window is not inherited by the child window in any way.</p> <p>You've said in the comments than when you pass the array from the main window to the child window the additions "disappear." (There's nothing in your code showing you doing that.) We can see from the above that in fact, the object maintains its relationship with <em>its</em> prototype correct and does show the additions, so I suspect (with apologies) it's just an error in your code.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload