Javascript Prototypal Inheritance Doubt II [英] Javascript Prototypal Inheritance Doubt II

查看:96
本文介绍了Javascript Prototypal Inheritance Doubt II的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了更好地理解它,我在js做了一些继承,我发现了让我感到困惑的东西。

I'been doing some inheritance in js in order to understand it better, and I found something that confuses me.

我知道当你调用'构造函数时函数'使用new关键字,你得到一个新对象,引用该函数的原型。

I know that when you call an 'constructor function' with the new keyword, you get a new object with a reference to that function's prototype.

我也知道为了进行原型继承,你必须更换原型构造函数的函数与你想成为'超类'的对象的实例。

I also know that in order to make prototypal inheritance you must replace the prototype of the constructor function with an instance of the object you want to be the 'superclass'.

所以我做了这个愚蠢的例子来尝试这些概念:

So I did this silly example to try these concepts:

function Animal(){}
function Dog(){}

Animal.prototype.run = function(){alert("running...")};

Dog.prototype = new Animal(); 
Dog.prototype.bark = function(){alert("arf!")};

var fido = new Dog();
fido.bark() //ok
fido.run() //ok

console.log(Dog.prototype) // its an 'Object' 
console.log(fido.prototype) // UNDEFINED
console.log(fido.constructor.prototype == Dog.prototype) //this is true

function KillerDog(){};
KillerDog.prototype.deathBite = function(){alert("AAARFFF! *bite*")}

fido.prototype = new KillerDog();

console.log(fido.prototype) // no longer UNDEFINED
fido.deathBite(); // but this doesn't work!

(这是在Firebug的控制台中完成的)

(This was done in Firebug's console)

1)为什么如果所有新对象都包含对creator函数原型的引用,fido.prototype是未定义的?

1) Why if all new objects contain a reference to the creator function's prototype, fido.prototype is undefined?

2)是继承链[obj] - > [constructor] - > [prototype]而不是[obj] - > [prototype]?

2) Is the inheritance chain [obj] -> [constructor] -> [prototype] instead of [obj] -> [prototype] ?

3)是我们对象(fido)的'prototype'属性?如果是这样的话......为什么'deathBite'未定义(在最后一部分)?

3) is the 'prototype' property of our object (fido) ever checked? if so... why is 'deathBite' undefined (in the last part)?

谢谢!

推荐答案


1)为什么如果所有新对象都包含对创建者函数的
原型的
引用,则fido.prototype为
undefined ?

1) Why if all new objects contain a reference to the creator function's prototype, fido.prototype is undefined?

所有新对象都包含对构造时构造函数中存在的原型的引用。但是,用于存储此引用的属性名称不是 prototype ,因为它在构造函数本身上。一些Javascript实现允许通过某些属性名称访问此隐藏属性,如 __ proto __ 其他人不这样做(例如微软)。

All new objects do hold a reference to the prototype that was present on their constructor at the time of construction. However the property name used to store this reference is not prototype as it is on the constructor function itself. Some Javascript implementations do allow access to this 'hidden' property via some property name like __proto__ where others do not (for example Microsofts).


2)是继承链[obj] - >
[constructor] - > [prototype]而不是
的[obj] - > [prototype] ?

2) Is the inheritance chain [obj] -> [constructor] -> [prototype] instead of [obj] -> [prototype] ?

没有。看看这个: -

No. Take a look at this:-

function Base() {}
Base.prototype.doThis = function() { alert("First"); }

function Base2() {}
Base2.prototype.doThis = function() { alert("Second"); }

function Derived() {}
Derived.prototype = new Base()

var x = new Derived()

Derived.prototype = new Base2()

x.doThis();

此警告First不是秒。如果继承链通过构造函数进行,我们将看到Second。当构造一个对象时,函数prototype属性中保存的当前引用被转移到对其原型的对象隐藏引用。

This alerts "First" not Second. If the inheritance chain went via the constructor we would see "Second". When an object is constructed the current reference held in the Functions prototype property is transfered to the object hidden reference to its prototype.


3)是我们的
对象(fido)的'prototype'属性曾经检查过吗?如果是这样的话...
为什么'deathBite'未定义(在
最后部分)?

3) is the 'prototype' property of our object (fido) ever checked? if so... why is 'deathBite' undefined (in the last part)?

分配对于一个对象(函数除外),一个名为 prototype 的属性没有特殊含义,如前所述,对象不通过这样的属性名维护对其原型的引用。

Assigning to an object (other than a Function) a property called prototype has no special meaning, as stated earlier an object does not maintain a reference to its prototype via such a property name.

这篇关于Javascript Prototypal Inheritance Doubt II的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆