js 判断数组的7 种方法

1. Array.isArray([]) // true
2. Object.prototype.toString.call([]) // '[object Array]'
3. [].constructor ===Array // true
4. [] instanceof Array // true
5. [].__proto__ === Array.prototype  // true
6. Array.prototype.isPrototypeOf([])  // true
7. Object.getPrototypeOf([]) === Array.prototype // true

以上方法中,1和 2 绝对靠谱,其他方法会受原型链被修改的影响,使用时注意。

posted @ 2022-08-18 01:20  蓓蕾心晴  阅读(182)  评论(0编辑  收藏  举报