判断一个JS对象是否为数组的方法

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/HHH_LLL/article/details/89878787

有以下三种方法:

一:使用instanceof

var arr=[1,2,3];
console.log(arr instanceof Array);
//true

二:使用原型链方法

var arr=[1,2,3];
console.log(arr.constructor === Array);
//true

三:自写一个封装函数,将判断功能加入其中

var arr=[1,2,3];
function isArray(o){
    return Object.prototype.toString.call(o) === '[object Array]';
}
console.log(isArray(arr));
//true

猜你喜欢

转载自blog.csdn.net/HHH_LLL/article/details/89878787