首页 > 技术文章 > JS 数组类型的判断方法

frontend-coder 2019-04-04 14:11 原文

1. array instanceOf Array

2. Array.prototype.isPrototype(array)

3. Object.getPrototype(array) === Array.prototype

4. array.constructor === Array

5. Object.prototype.toString.call(array) === '[object Array]'

6. Array.isArray(array)

 

PS: 

  [].toString()  === "";  (注意不是 " " 或者 ' ');

  Object.prototype.toString.call([]) === '[object Array]';

  两个值的不同是因为在 Array.prototype 里面对 toString 方法进行了重写;  Array.prototype.hasOwnproperty('toString') === true

推荐阅读