我知道一种判断数组的方法是typeof arr instanceof Array.但是在网上看到说什么在不同框架下就不能这么判断?什么iframe的,怎么样才算不一样的框架?能举个例子吗?
v instanceof Array will return false if v was created in another frame (v is instance of thatFrame.contentWindow.Array class).
这个好啊,以前还真不知道,这会儿赶紧恶补一下!!!
一个例子:
var iframe = document.createElement('iframe');
document.body.appendChild(iframe);
xArray = window.frames[window.frames.length-1].Array;
var arr = new xArray(1,2,3); // [1,2,3]
// Boom!
arr instanceof Array; // false
// Boom!
arr.constructor === Array; // false
原文地址:)
用instanceof typeof 本来就不是一个好方法
Object.prototype.toString.call(arr).toUpperCase() === "[OBJECT ARRAY]"
javascript
Object.prototype.toString.apply([]).slice(8,-1)