如何判断一个数组对象是数组对象的几种方法

  1. 利用Object的tostring方法来判断,原理是调用这个对象下的方法可以输出[object 构造函数],具体用法如下:

    Obejct.prototype.tostring.call(需要判端的对象)

  2. 使用instance,instance的作用就是判断左边对象是不是等于右边对象,用法如下:
    var arr=[];
    console.lof(arr instanceof Array)

  3. 使用ES6新增的数组方法:
    console.log(Array.isArray(arr));

  4. 使用constructor方法,利用实例化对象与构造函数之间的联系判断:
    console.log(arr.constructor.name=='Array')

  5. 使用数组的下的push方法来判断
    console.log(arr.push!=undefined)

posted @ 2017-08-05 19:15  angeliaxu  阅读(345)  评论(0编辑  收藏  举报