Javascript原型继承? [英] Javascript Prototypal Inheritance?

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

问题描述

为了更好地理解它,我在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)为什么如果所有新对象都包含对创建者函数原型的引用,则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是 未定义?

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

所有新对象的确包含对构造时存在于其构造函数上的原型的引用.但是,用于存储此引用的属性名称不是prototype,而是在构造函数本身上.某些Javascript实现确实允许通过某些属性名称(例如__proto__)访问此隐藏"属性,而其他某些属性则不允许(例如Microsoft).

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]-> [构造函数]-> [原型]代替 [obj]-> [prototype]的?

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

不.看看这个:-

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();

这将警告第一"而不是第二.如果继承链通过构造函数,我们将看到第二个".构造对象时,函数"原型属性中保存的当前引用将转移到其原型的对象隐藏引用.

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)是否曾经检查过?如果是这样... 为什么"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原型继承?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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