JS 数组类型的判断方法

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

转载于:https://www.cnblogs.com/frontend-coder/p/10654572.html

你可能感兴趣的:(JS 数组类型的判断方法)