Type checking for array

This question already has an answer here:

  • How do you check if a variable is an array in JavaScript? [duplicate] 24 answers

  • You could use

    yourThing.constructor === Array
    

    This returns true if yourThing is an array.

    So you don't have to change Array's prototype. Modifying objects you don't own is widely seen as bad practice.


    您可以使用instanceof (MDN文档):

    var arr = [];
    if (arr instanceof Array) {
      console.log('this is an array');
    }
    
    链接地址: http://www.djcxy.com/p/19304.html

    上一篇: 如何检查目标变量是否在数组中?

    下一篇: 检查数组类型