in.html
958 Bytes
<html>
<head>
</head>
<body>
<script>
function Person(){
};
Person.prototype.name = "hello";
Person.prototype.age = "12";
Person.prototype.sayName = function(){alert(this.name);};
function hasPrototypeProperty(object, name){
//同时使用hasOwnProperty()方法和in操作符,就可以确定该属性到底是存在于对象中还是原型中
//hasOwnProperty()函数用于指示一个对象自身(不包括原型链)是否具有指定名称的属性。如果有,返回true,否则返回false。
return !object.hasOwnProperty(name) && (name in object);
};
var p1 = new Person();
<!-- console.log("p1",p1,p1.name) -->
<!-- console.log("10",p1.hasOwnProperty("name"),("name" in p1)); -->
console.log("1",hasPrototypeProperty(p1,"name")); //true
p1.name = "nn";
<!-- console.log("20",p1.hasOwnProperty("name"),("name" in p1)); -->
console.log("2",hasPrototypeProperty(p1,"name")); //false;
</script>
</body>
</html>