1.判斷對象的constructor是否指向Array,接着判斷特殊的屬性length,splice等。[應用的是constructor的定義:返回對象所對應的構造函數。]
eg: [].constructor == Array; //true
2.使用instanceof 判斷對象是否是數組的實例,[應用的是instanceof的定義:某實例是否是某構造函數的實例],[極端情況下不行,比如iframe的嵌套頁面之間的判斷],[Array實質上是window.Array]
eg: var arr = []; console.log(arr instanceof Array); //返回true。
3.[最優方式]使用Object.prototype.toString().call(arr) === "[object Array]"
4.ES5中定義了Array.isArray(arr)方法,返回true/false。
對於不兼容isArray()方法的,可以用下面這個代碼:
if(typeof Array.isArray === "undefined"){
Array.isArray = function(arg){
return Object.prototype.toString.call(arg) === "[object Array]";
};
}
參考鏈接:https://www.cnblogs.com/linda586586/p/4227146.html
https://blog.csdn.net/u010297791/article/details/55049619