判断是否是一个数组?

1. instanceof

var a = {
    "name":'suonidfahao',
    "job": 'webEnginner',
}   
var b = [1,2,4,4];
console.log(a instanceof Array); //false
console.log(b instanceof Array); //true

2.Array.isArray(ie9以上支持)

Array.isArray([1, 2, 3]);  // true
Array.isArray({foo: 123}); // false
Array.isArray('foobar');   // false
Array.isArray(undefined);  // false

3.原型链:constructor

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

posted on 2018-07-12 09:45  向往回得来  阅读(103)  评论(0编辑  收藏  举报

导航