1.hasOwnProperty方法
hasOwnProperty判断可以只能判断对象自身有没有这个属性,无法判断对象原型上面有没有这个属性。
function Person(){
this.username = 'jack'
this.age = 18
}
Person.prototype.gender = '男'
const p1 = new Person()
console.log(p1)
console.log(p1.hasOwnProperty('age')) // true
console.log(p1.hasOwnProperty('gender')) // false
2.in属性
in既可以判断对象自身有没有这个属性,也可以判断对象原型上面有没有这个属性。
function Person(){
this.username = 'jack'
this.age = 18
}
Person.prototype.gender = '男'
const p1 = new Person()
console.log(p1)
console.log('age' in p1) // true
console.log('gender' in p1) // true