instanceof 和in 和hasOwnProperty

instanceof /in /hasOwnProperty

instanceof:检测某一个实例是否隶属于这个类

function Fn() {
    var n = 10;
    this.m = n;
  
var f = new Fn()
// console.log(f instanceof Fn);//=>TRUE
// console.log(f instanceof Array);//=>FALSE
// console.log(f instanceof Object);//=>TRUE (万物皆对象:所有的对象,包含创建的实例都是Object的实例)

in:检测当前对象是否存在某个属性(不管当前这个属性是对象的私有属性还是公有属性,只要有结果就是TRUE)

// console.log('m' in f);//=>TRUE
// console.log('n' in f);//=>FALSE
// console.log('toString' in f);//=>TRUE toString是它的公有属性

hasOwnProperty:检测当前属性是否为对象的私有属性(不仅要有这个属性,而且必须还是私有的才可以)

 console.log(f.hasOwnProperty('m'));//=>TRUE
// console.log(f.hasOwnProperty('n'));//=>FALSE 连这个属性都没有
// console.log(f.hasOwnProperty('toString'));//=>FALSE 虽然有这个属性但是不是私有的属性

发布了9 篇原创文章 · 获赞 2 · 访问量 55

猜你喜欢

转载自blog.csdn.net/weixin_43324909/article/details/104571630