[英].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
旁注:您设置继承链的方式很常见,并在很多示例中显示,但在两个方面不正确:
您不想使用new Human
来创建Male.prototype
。
你想叫Human
从Male
:
所以:
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 Human
为Male
创建原型的原因很简单: 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.prototype
是Human
一个实例
因为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.