判断一个变量是数组还是对象
- 使用 instanceof
使用 isArray
var arr = []
var obj = {}// instanceof
function ins(arr) {if (arr instanceof Array) {
console.log('is array')
} else {
console.log('not array')
}
}
ins(arr) // ‘is array’
ins(obj) // ‘not array’// isArray
function is(arr) {
if (Array.isArray(arr)) {console.log('is array')
} else {
console.log('not array')
}
}
is(arr) // ‘is array’
is(obj) // ‘not array’
转载于//www.cnblogs.com/0x29a/p/11240874.html
还没有评论,来说两句吧...