首页 > 编程开发 > JavaScript    日期:2023-04-13 / 浏览

目录

instanceof

主要用来判断某个实例是否属于某个对象所在的原型链上,因此并不能完全分辨出到底是否是数组

  let a = [1, 2, 3];
      console.log(a instanceof Array); // true
      console.log(a instanceof Object); // true
      //从此我们可以看出a既是数组,也是对象
 
  let userInfo = { userName: "zhangsan" };
      console.log(userInfo instanceof Array); // false
      console.log(userInfo instanceof Object); // true
      //userInfo只是对象,而不是数组

Array.isArray()

Array.isArray([1,2]); // true
Array.isArray({name:'zs'}); // false

constructor构造函数

let a = [1,2];
a.__proto__.constructor === Array // true
a.__proto__.constructor === Object // false

a.constructor === Array // true
a.constructor === Object // false

toString

Object.prototype.toString.call([1,2]) // '[object Array]'
Object.prototype.toString.call({name:'zs'}) // '[object Object]'

isPrototypeOf

Array.prototype.isPrototypeOf([1,2]) // true
Array.prototype.isPrototypeOf({name:'zs'})  // false

getPrototypeOf

Object.getPrototypeOf([1,2]) === Array.prototype // true
Object.getPrototypeOf({name:'zs'}) === Array.prototype // false

觉得上面的内容有用吗?快来点个赞吧!

点赞() 我要打赏

温馨提示 : 本站内容来自会员投稿以及互联网,所有源码及教程均为作者总结编辑,请大家在使用过程中提前做好备份,以免发生无法预知的错误,源码类教程请勿直接用于生产环境!

 可能感兴趣的文章