深入解析hasOwnProperty与isPrototypeOf

这里采用一个实例来说明:

 1 function Person(name) {
 2             //以下都是Person的OwnProperty
 3             this.name = name;
 4             this.showMe = function () {
 5                 alert(this.name);
 6             };
 7         }
 8         //每一个'类'都有prototype属性,而这里的protype指向的是一个prototype对象,所以这里的prototype不是OwnProperty
 9         Person.prototype.from = function () {
10             alert("I come from prototype");
11         };
12         var father = new Person('js');
13 
14         /*object.hasOwnProperty(proName); 
15         判断proName的名称是不是object对象的一个属性或对象*/
16         alert(father.hasOwnProperty("name"));//true
17         alert(father.hasOwnProperty("from"));//false
18         alert(Person.prototype.hasOwnProperty("name"));//false
19         alert(Person.prototype.hasOwnProperty("from"));//true
20 
21         /*object1.isPrototypeOf(object2);
22         对象object1是否存在于另一个对象object2的原型链中*/
23         alert(Person.prototype.isPrototypeOf(father));//true
24         //【因为Person.prototype只有constructor,from,但father里有name,showMe之外还有constructor,from】
View Code

如如图所示:Person.prototype在father的原型链中

是不是很容易就理解了呢!~_~

原文地址:https://www.cnblogs.com/pfcan66/p/3507099.html