首页 > 其他 > 详细

hasOwnProperty与in的区别

时间:2020-07-16 15:51:22      阅读:62      评论:0      收藏:0      [点我收藏+]

1、hasOwnProperty只能判断是否是属于自身的属性,无法找到原型身上的属性(hasOwnProperty()只在属性存在于实例中时才返回true

 

Person.prototype.lastName = "Deng";

function Person() {
}

var person = new Person();
person.age = 12;

if (person.hasOwnProperty(‘lastName‘)) {
    //找不到不执行
    console.log(person.lastName)
}

if (person.hasOwnProperty(‘age‘)) {
    //能找到会输出12
    console.log(person.age)
}

 

2、in原型身上的属性也能找到(in操作符只要通过对象能访问到属性就返回true

console.log(‘lastName‘in person)
//返回true

 

hasOwnProperty与in的区别

原文:https://www.cnblogs.com/h5it/p/13322159.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!