繁体   English   中英

.isPrototypeOf()和.hasOwnProperty()方法混淆

[英].isPrototypeOf() and .hasOwnProperty() method confusion

假设我有这个代码:

 // Male will inherit ALL of the Human properties function Human(x, y) { // Following properties will be inherited this.name = x; this.age = y; this.test = "Test 1"; } // Following properties will ALSO be inherited Human.prototype.citizen = "USA"; Human.prototype.employer = "Google"; Human.prototype.test = "Test 2"; function Male(x, y) { // Following properties will be the own properties of Male instances this.name = x; this.age = y; this.gender = "Male"; } // Inheritance - Connecting Male object with Human object Male.prototype = new Human(); // no arguments are passed Male.prototype.constructor = Male; // correcting constructor property var albert = new Male("Albert", 25); 

然后,我想对代码进行一些测试

Human.isPrototypeOf(albert); // I expect it to return TRUE

但它返回FALSE,为什么呢?

并且,对于以下测试

Human.hasOwnProperty("age"); // /i expect it to return TRUE

但它返回FALSE,为什么呢?

谢谢,

编辑我的问题与其他问题略有不同,因为它也谈论原型链。

Human功能不在 albert的原型链中。 Human.prototype引用的对象是:

Human.prototype.isPrototypeOf(albert); // true

Human.hasOwnProperty( “时代”); //我希望它返回TRUE

Human函数没有age属性。 new创建的实例:

new Human().hasOwnProperty("age"); // true

旁注:您设置继承链的方式很常见,并在很多示例中显示,但在两个方面不正确:

  1. 您不想使用new Human来创建Male.prototype

  2. 想叫HumanMale

所以:

function Male(x, y) {
    // Give Human its chance to initialize the object (#2)
    Human.call(this, x, y);
    // ...
}

// Don't use new Human to create the prototype (#1)
Male.prototype = Object.create(Human.prototype);
Male.prototype.constructor = Male;

你不使用new HumanMale创建原型的原因很简单: Human期望参数,但你没有任何东西可以给它。

这是更新的ES5及该代码的早期版本:

 function Human(name, age) { // Argument names should be meaningful this.name = name; this.age = age; this.test = "Test 1"; } Human.prototype.citizen = "USA"; Human.prototype.employer = "Google"; Human.prototype.test = "Test 2"; function Male(name, age) { Human.call(this, name, age); this.gender = "Male"; } Male.prototype = Object.create(Human.prototype); Male.prototype.constructor = Male; var albert = new Male("Albert", 25); console.log(Human.prototype.isPrototypeOf(albert)); // true console.log(new Human().hasOwnProperty("age")); // true 

或者当然,使用ES2015 +(如果您的目标尚不支持,请进行转换):

 // THIS SNIPPET REQUIRES A BROWSER WITH ES2015+ SUPPORT class Human { constructor(name, age) { this.name = name; this.age = age; this.test = "Test 1"; } } Human.prototype.citizen = "USA"; Human.prototype.employer = "Google"; Human.prototype.test = "Test 2"; class Male extends Human { constructor(name, age) { super(name, age); this.gender = "Male"; } } let albert = new Male("Albert", 25); console.log(Human.prototype.isPrototypeOf(albert)); // true console.log(new Human().hasOwnProperty("age")); // true 


你已经说过你正在试图看看链是如何运作的。 这是创建albert后我们在内存中的图表(为简单起见,删除了一些细节):

+−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−+
         |                                                                      |
         \ +−−−−−−−−−−−−−−−−+                                                   |
Human−−−−−>|    function    |                                                   |
           +−−−−−−−−−−−−−−−−+                            +−−−−−−−−−−−−−−−−−−−−+ |
           | prototype      |−−−−−−−−−−−−−−−−−−−−−−−−−−−>|       object       | |    
           | name: "Human"  |                          / +−−−−−−−−−−−−−−−−−−−−+ |    
           +−−−−−−−−−−−−−−−−+                          | | constructor        |−+
                                                       | | citizen: "USA"     |
         +−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−+ | | employer: "Google" |
         |                                           | | | test: "Test 2"     |
         \ +−−−−−−−−−−−−−−−−+                        | | +−−−−−−−−−−−−−−−−−−−−+
Male−−−−−−>|    function    |                        | |
           +−−−−−−−−−−−−−−−−+    +−−−−−−−−−−−−−−−−−+ | |
           | prototype      |−−−>|     object      | | |
           | name: "Male"   |  / +−−−−−−−−−−−−−−−−−+ | |
           +−−−−−−−−−−−−−−−−+  | | constructor     |−+ |
                               | | [[Prototype]]   |−−−+
           +−−−−−−−−−−−−−−−−+  | +−−−−−−−−−−−−−−−−−+
albert−−−−>|     object     |  |
           +−−−−−−−−−−−−−−−−+  |
           | name: "Albert" |  |
           | age: 25        |  |
           | gender: "Male" |  |
           | [[Prototype]]  |−−+
           +−−−−−−−−−−−−−−−−+

[[Prototype]]是规范用于包含对其原型对象的引用的对象的“内部槽”的名称。 相比之下, prototype ,函数上的属性(例如, Human.prototype ),只是函数的一个普通属性,它指向new将用作它创建的新对象的[[Prototype]]的对象,如果你使用它功能与new

该图表上的一些注释:

  • 所有函数都有一个[[Prototype]]内部插槽,指向Function.prototype指向的对象(为简单起见,上面省略)。
  • Human ,这个函数,有一个name属性: "Human"
  • Male ,功能,有一个name属性: "Male"
  • 对象albert指的是一个name属性: "Albert"
  • albert[[Prototype]]Male.prototype ; Male.prototype[[Prototype]]Human.prototype (和Human.prototype[[Prototype]] ,未显示,是`Object.prototype)。

你在评论中说过:

我只是无法理解为什么继承语句后我们可以做Male.prototype.isPrototypeOf(albert)返回true而不是Human.isPrototypeOf(albert) (它返回false )因为Male.prototypeHuman一个实例

因为Human ,这个功能,在albert的原型链中是无处可去的。 让我们看看albert的原型链:

  • albert[[Prototype]]Male.prototype
  • Male.prototype[[Prototype]]Human.prototype
  • Human.prototype[[Prototype]]Object.prototype
  • Object.prototype[[Prototype]]null

如图:

+−−−−−−−−−−−−−−−+    +−−−−−−−−−−−−−−−−+    +−−−−−−−−−−−−−−−−−+    +−−−−−−−−−−−−−−−−−−−−−+ 
|    albert     |    | Male.prototype |    | Human.prototype |    |  Object.prototype   |
+−−−−−−−−−−−−−−−+    +−−−−−−−−−−−−−−−−+    +−−−−−−−−−−−−−−−−−+    +−−−−−−−−−−−−−−−−−−−−−+ 
| [[Prototype]] |−−−>| [[Prototype]]  |−−−>| [[Prototype]]   |−−−>| [[Prototype]]: null |
+−−−−−−−−−−−−−−−+    +−−−−−−−−−−−−−−−−+    +−−−−−−−−−−−−−−−−−+    +−−−−−−−−−−−−−−−−−−−−−+

因此, Human ,这个功能,在这个链条中无处可去。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM