区别对象和数组的方法

区别对象和数组的方法

1、constructor
  var obj = {};
  obj.constructor;//object
    [].constructor;//Array
2、obj instanceof Array; true 就是数组 false 就是对象
    A instanceof B 他是看A的原型链上有没有B的原型
3、toString 方法 (推荐使用)
    object.prototype.toString.call([]);//打印的是"[object Array]"
    object.prototype.toString.call({});//打印的是"[object Object]"
4、可以使用 isArray()来判断
  Array.isArray([]); //true
  Array.isArray({}); //false

猜你喜欢

转载自blog.csdn.net/Ultraman_and_monster/article/details/80956001
今日推荐