js判断变量是不是数组

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


//
Object.prototype.toString.call([]) '[object Array]'
//Object.prototype.toString.call({})             '[object Object]'
//Object.prototype.toString.call(1)              '[object Number]'
//Object.prototype.toString.call('1')            '[object String]'
//Object.prototype.toString.call(null)           '[object Object]'
//Object.prototype.toString.call(undefined)      '[object Undefined]'

 

posted @ 2020-06-20 11:38  Peter_Yang0942  阅读(284)  评论(0编辑  收藏  举报