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

posted @ 2019-04-04 14:11  一步一步跑  阅读(988)  评论(0编辑  收藏  举报