Array.isArray
代码均来自:MDN
//利用Object.prototype.toString实现类型判断
if (!Array.isArray) {
Array.isArray = function(arg) {
return Object.prototype.toString.call(arg) === '[object Array]';
};
}
- Array.prototype 也是一个数组
Array.isArray(Array.prototype); // true
- Array.isArray 优于 instanceof
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]
// Correctly checking for Array
Array.isArray(arr); // true
// Considered harmful, because doesn't work though iframes
arr instanceof xArray;
// false 这里输出false,我觉得是因为在引用iframes的Array构造函数时
// 给他令起了名字为xArray,arr instanceof xArray 为true
// 因为此时arr是由xArray构造函数实例化的,但其本质还是一个数组