hasOwnProperty方法是用来检测当前属性是否为对象的私有属性(不仅要有这个属性,而且必须还是私有的才可以),该方法仅用于判断自身对象,不会查找原型链。
function Person() {
this.name = "坤坤"
}
person.prototype.age = 18;//在原型对象上添加age属性
const person = new Person();
console.log(person.hasOwnProperty("age")); //false
console.log(person.hasOwnProperty("name")); //true
person.name = "张三";//在实例本身添加name属性
console.log(person.hasOwnProperty("name")); //true