Why is myarray instanceof Array and myarray.constructor === An array as false when myarray is in a frame?

So, the following code warns a lie twice:

window.onload = function(){ alert(window.myframe.myarray instanceof Array); alert(window.myframe.myarray.constructor === Array); } 

When the page named "myframe" has an iframe that contains an array called "myarray". If the array moves to the main page (unlike the iframe), then the code warns true twice as expected. Does anyone know why this is?

+7
source share
2 answers
 function isArray(o) { return Object.prototype.toString.call(o) === '[object Array]'; } 

A long explanation here is why .constructor does not work with frames.

Problems arise when it comes to scripting in multi-frame DOM environments. In a nutshell, Array objects created inside one iframe do not share [[Prototype]] s with arrays created in another iframe. Their constructors are different objects, therefore, instance and constructor checks are not performed:

+18
source

Two windows create their own global script environment.

The array constructor of one object is not the same object as another.

 var win2=window.myframe; alert(win2.myarray instanceof win2.Array); returns true 
+4
source

All Articles