当myarray在一个框架中时,为什么myarray instanceof Array和myarray.constructor === Array都为false? [英] Why are myarray instanceof Array and myarray.constructor === Array both false when myarray is in a frame?

查看:340
本文介绍了当myarray在一个框架中时,为什么myarray instanceof Array和myarray.constructor === Array都为false?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码警告错误两次:

So the following code alerts false twice:

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

当名为myframe的页面中有一个包含数组的iframe时叫做myarray。如果将数组移动到主页面(而不是iframe),则代码会按预期警告两次。有谁知道这是为什么?

When there's an iframe in the page named "myframe" that has contains an array called "myarray". If the array is moved into the main page (as opposed to the iframe), then the code alerts true twice as expected. Does anyone know why this is?

推荐答案

function isArray(o) {
  return Object.prototype.toString.call(o) === '[object Array]';
}

长解释这里关于为什么 .constructor 失败了frame。

Long explanation here about why .constructor fails with frames.


在多帧DOM环境中编写脚本时会出现问题。简而言之,在一个iframe中创建的Array对象不与另一个iframe中创建的数组共享[[Prototype]]。它们的构造函数是不同的对象,因此instanceof和构造函数检查都失败了:

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:

这篇关于当myarray在一个框架中时,为什么myarray instanceof Array和myarray.constructor === Array都为false?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆