前言

JavaScript中关于数组的判定问题,一直都是一个必须要掌握的点,那么,运用知识,如何判断一个类型是数组,就需要有对JavaScript使用有着深入的了解。

判断方法

一、Array.isArray

ES5新增的数组方法,Array.isArray应该是我们最先想到的判定方式,现在应用的也比较广泛。

const arr = [1,2,3,4]
Array.isArray(arr)    // true

如果是在支持ES5的浏览器中,用这样的方式自然没什么问题,但是如果是在IE9以下版本,这个方法并未受到支持,这时候我们就要用别的方式来判断数组。

二、instanceof

一般来说,instanceof关键字,是用来判断某个元素是否某对象构造函数实例。在数组判断上,instanceof关键字也可以进行数组的判定。

const arr = [1, 2, 3, 4]
arr instanceof Array   // true

instanceof支持的浏览器版本比较多,所以一般来说,用instanceof判断,会比Array.isArray判定的范围要广泛。

三、toString

对象的toString方式也可以判定数组类型,一般来说这种方式的判定是各大库的一种Array.isArray的代替实现。

例如,polyfill中,就是如此实现:

if (!Array.isArray) {
  Array.isArray = function(arg) {
    return Object.prototype.toString.call(arg) === '[object Array]';
  };
}

const arr = [1, 2, 3, 4]
Object.prototype.toString.call(arr) === '[Object Array]'   // true

四、constructor

除了以上的方式之外,我们还可以通过构造函数来判定:

const arr = [1, 2, 3, 4]
arr.constructor === Array  // true
arr.__proto__.constructor === Array  //true

弊端

instanceof和constructor的判定也存在一些弊端,他们判定的数组必须定义在同一个页面,否则将会判定为false。

如果在iframe中的数组判定,就会出错。

var iframe = document.createElement('iframe');
document.body.appendChild(iframe);
xArray = window.frames[window.frames.length-1].Array;
var arr = new xArray(1,2,3); // [1,2,3]

// Correctly checking for Array
Array.isArray(arr);  // true
// Considered harmful, because doesn't work through iframes
arr instanceof Array; // false
arr.constructor === Array; // false

总结

由上述几个方法判定,可以得出,其实polyfill的判定是最合理的,也最具有兼容性的一种判定。

利用toString判定,胜出。

133 thoughts on “JavaScript中如何判断数组类型

  1. Larrylubre说道:

    Terrific forum posts. Appreciate it! azithromycine

  2. PhillipSwark说道:

    You made your stand pretty clearly!! canada drugs online

  3. Larrylubre说道:

    You actually revealed this superbly! Fluoxetine And Zoloft

  4. PhillipSwark说道:

    Seriously all kinds of amazing data! cbd oil for pain

  5. Richardundip说道:

    Amazing plenty of helpful information. cheap viagra

  6. TyroneMerne说道:

    You’ve made your position extremely nicely!. viagra pills

  7. PhillipSwark说道:

    Whoa all kinds of terrific info. best hemp oil

  8. Louiscoutt说道:

    Fantastic information, With thanks. provigil lawsuit

  9. Larrylubre说道:

    Seriously all kinds of terrific information. celebrex

  10. ErnestCunda说道:

    You actually stated that adequately. prednisone for dogs

  11. TyroneMerne说道:

    Excellent advice. Thanks a lot! buy modafinil

  12. ErnestCunda说道:

    Thanks a lot. Plenty of knowledge!
    kamagra

  13. Louiscoutt说道:

    You actually revealed it adequately! drugs online

  14. ErnestCunda说道:

    Cheers! Terrific information! prednison

  15. TyroneMerne说道:

    Seriously all kinds of very good knowledge. provigil medication

  16. TyroneMerne说道:

    You actually explained it effectively. london drugs canada

  17. ErnestCunda说道:

    Wow all kinds of valuable advice! baclofen pump

  18. Louiscoutt说道:

    You’ve made your position pretty effectively.! aarp recommended canadian pharmacies

  19. TyroneMerne说道:

    Seriously a lot of excellent facts! viagra

  20. Larrylubre说道:

    You made your position very effectively.! Topical Keflex

  21. Louiscoutt说道:

    You actually revealed it really well! drugs for sale

  22. ErnestCunda说道:

    Truly many of valuable information. tamsulosine

  23. TyroneMerne说道:

    You have made your point extremely nicely.. Cost Cialis Once Daily

  24. Larrylubre说道:

    Amazing loads of great knowledge. Acyclovir Tablets

  25. TyroneMerne说道:

    You’ve made your position extremely effectively!! viagra generic

  26. ErnestCunda说道:

    Truly a lot of excellent info. bactrim ds

  27. Louiscoutt说道:

    Incredible many of superb tips. canadian rx

  28. Richardundip说道:

    You actually suggested this fantastically. how to boost viagra effects

  29. Richardundip说道:

    You actually suggested this adequately! cbd hemp oil walmart

发表评论