gpt4 book ai didi

javascript - 当 myarray 在一个框架中时,为什么 myarray instanceof Array 和 myarray.constructor === Array 都为 false?

转载 作者:数据小太阳 更新时间:2023-10-29 04:24:31 26 4
gpt4 key购买 nike

所以下面的代码会发出两次错误警报:

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

当页面中有一个名为“myframe”的 iframe 包含一个名为“myarray”的数组时。如果数组被移动到主页(而不是 iframe),那么代码会像预期的那样发出两次 true 警报。有谁知道这是为什么吗?

最佳答案

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

详细解释 here关于为什么 .constructor 因框架而失败。

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:

关于javascript - 当 myarray 在一个框架中时,为什么 myarray instanceof Array 和 myarray.constructor === Array 都为 false?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6473273/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com