判断某一个对象是否拥有某一个属性
in的特点: 只要类中或者原型对象中有, 就会返回true
class Person{
name = null;
age = 0;
}
Person.prototype.height = 0;
let per = new Person();
console.log("name" in per ); // true
console.log("width" in per ); // false
console.log("height" in per ); // true
判断某一个对象自身是否拥有某一个属性
hasOwnProperty()方法只会去类中查找有没有, 不会去原型对象中查找
作为参数的属性名必须以字符串形式指定
class Person{
name = null;
age = 0;
}
Person.prototype.height = 0;
let per = new Person();
console.log(per.hasOwnProperty("name")); // true
console.log(per.hasOwnProperty("height")); // false