hasOwnProperty()
表示是否有自己的属性。这个方法会查找一个对象是否有某个属性
但是不会去查找它的原型链。
let obj = {
name:'张三',
fn:function (){
},
hobby:['篮球','足球','乒乓球'],
where:{
province:'北京',
city:'北京',
}
}
console.log(obj.hasOwnProperty('name')); //true
console.log(obj.hasOwnProperty('fn')); //true
console.log(obj.hasOwnProperty('hobby')); //true
console.log(obj.where.hasOwnProperty('city')); //true
console.log(obj.hasOwnProperty('abc')); //false
let obj1 = new Object()
console.log(obj1.hasOwnProperty('toString')); //false
console.log(Object.prototype.hasOwnProperty('toString')); //true
// toString方法是Object这个对象的方法,obj1对象本身是没有这个toString这个属性的