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