<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<script type="text/javascript">
class Person
{
name=null;
age=0;
}
Person.prototype.height=0;
/*let p=new Person();
// in的特点: 只要类中或者原型对象中有, 就会返回true
console.log("name" in p);
console.log("width" in p); // false
console.log("height" in p); // true
*/
// 需求: 判断某一个对象自身是否拥有某一个属性
let p = new Person();
// 特点: 只会去类中查找有没有, 不会去原型对象中查找
console.log(p.hasOwnProperty("name"));
console.log(p.hasOwnProperty("height"));
</script>
</body>
</html>
hasOwnProperty
最新推荐文章于 2023-03-13 17:58:07 发布