js判断数据类型

2020-05-15  本文已影响0人  小光啊小光
let arr = [a: 1, b: 2]
let obj = {a: 1, b: 2}

typeof arr  // object 
arr instanceof Object // true
arr instanceof Array  // true
arr.constructor === Array // true
Array.isArray(arr) // true *推荐
Object.prototype.toString.call(arr) === '[object Array]' // true *推荐

typeof obj // object
obj instanceof Object // true
obj.constructor === Object // true
Object.prototype.toString.call(obj) === '[object Object]' // true *推荐
上一篇下一篇

猜你喜欢

热点阅读