首页 > 编程语言 > 详细

原生js 判断变量是一个数组

时间:2019-01-05 19:33:55      阅读:122      评论:0      收藏:0      [点我收藏+]

const arr = []
// 1. 最简单 ES5+
Array.isArray(arr)

// 2. 兼容性好的方法,也很准确 
Object.prototype.toString.call(arr) === ‘[object Array]‘

// 3. 这个也不错 
arr.constructor.name === ‘Array‘

// 4. instance  不好用 
console.log(a instanceof Array) // true 
console.log(a instanceof Object) // true 

// 5 找个数组独有的方法 
arr.splice !== undefined

原生js 判断变量是一个数组

原文:https://www.cnblogs.com/lovellll/p/10225588.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!