Why are myarray instanceof Array and myarray.constructor === Array both false when myarray is in a frame?

后端 未结 2 1340
醉酒成梦
醉酒成梦 2020-12-10 06:35

So the following code alerts false twice:

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


        
相关标签:
2条回答
  • 2020-12-10 06:38
    function isArray(o) {
      return Object.prototype.toString.call(o) === '[object Array]';
    }
    

    Long explanation here about why .constructor fails with frames.

    The problems arise when it comes to scripting in multi-frame DOM environments. In a nutshell, Array objects created within one iframe do not share [[Prototype]]’s with arrays created within another iframe. Their constructors are different objects and so both instanceof and constructor checks fail:

    0 讨论(0)
  • 2020-12-10 06:57

    The two windows each create their own global script environment.

    The Array constructor of one is not the same object as the other.

    var win2=window.myframe;
    alert(win2.myarray instanceof win2.Array); returns true
    
    0 讨论(0)
提交回复
热议问题