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


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM