hasOwnProperty和isPrototypeOf方法使用

hasOwnProperty():判断对象是否有某个特定的属性。必须用字符串指定该属性。(例如,o.hasOwnProperty("name"))  //复制自w3cschool

IsPrototypeOf(): 判断该对象是否为另一个对象的原型。

不过hasOwnProperty 可以被赋值 ,这是不保险的。所以不能完全信任 hasOwnProperty 检测结果。

function Person(name,color){
    this.name=name;
    this.color=color;
}
Person.prototype.sayName=function(){
    alert(this.name);
}

var oP = new Person("aa",'red');
var oP2 = new Person('bb','blue');
oP.age=30;

console.log(oP.hasOwnProperty('name'));   //true
console.log(oP.hasOwnProperty('color'));  //true
console.log(oP.hasOwnProperty('age'));    //true
console.log(oP.hasOwnProperty('sayName'));//false
console.log(Person.prototype.isPrototypeOf(oP)); //true
Now or nerver .
原文地址:https://www.cnblogs.com/iyueyao/p/3268399.html