简体   繁体   English

使用原型的JavaScript继承

[英]JavaScript inheritance using prototype

I've been programming for over 20 years, but have recently turned to JavaScript. 我已经编程超过20年,但最近转向JavaScript。 Despite spending hours trawling the web, the penny hasn't yet dropped with the prototype inheritance method. 尽管花了几个小时在网上搜索,但是使用原型继承方法还没有下降。

In the simplified code below, I am trying to inherit the 'name' property from the Synthesizer 'class' to the Roland 'class', but the only way I seem to be able to access it is by using 'Synth2.prototype.name' rather than by 'Synth2.name' (which returns undefined). 在下面的简化代码中,我试图从Synthesizer'类'继承'name'属性到Roland'类',但我似乎能够访问它的唯一方法是使用'Synth2.prototype.name '而不是'Synth2.name'(返回undefined)。 I would like to get the approach working so that I can use 'Synth2.name', as portability is a design requirement. 我想让方法有效,以便我可以使用'Synth2.name',因为可移植性是一个设计要求。

I would be very grateful for any assistance. 我将非常感谢任何帮助。

function Synthesizer(name) {
    this.name = name;
}

function Roland(name) {
    this.prototype = new Synthesizer(name);
}

Synth1 = new Synthesizer("Analogue");
Synth2 = new Roland("Fantom G6");

document.write(Synth1.name + '<br>');
document.write(Synth2.name + '<br>');

Thanks guys! 多谢你们! (Now updated with call to super class)... (现在通过调用超级类更新)...

function Synthesizer(name) {
    this.name = name;

    this.rendersound = function () {

        document.write("applying envelope to " + this.name + "<br>");

    }
}

function Roland(name) {
    Synthesizer.call(this, name);
    this.prototype = Synthesizer;

    this.Synthesizer_rendersound = this.rendersound;
    this.rendersound = function () {

        document.write("applying differential interpolation to " + this.name + "<br>");
        this.Synthesizer_rendersound(this);

    }

}

Synth1 = new Synthesizer("Analogue");
Synth2 = new Roland("Fantom G6");

document.write(Synth1.name + '<br>');
document.write(Synth2.name + '<br>');

document.write('<br>');
Synth1.rendersound();

document.write('<br>');
Synth2.rendersound();

document.write('<br>');
document.write('Synth1.prototype ' + Synth1.prototype + '<br>');
document.write('Synth2.prototype ' + Synth2.prototype + '<br>');

document.write('<br>');
document.write('Synth1.constructor ' + Synth1.constructor + '<br>');
document.write('Synth2.constructor ' + Synth2.constructor + '<br>');

I believe you have to set the constructor's prototype , like this: 我相信你必须设置构造函数的prototype ,如下所示:

function Synthesizer(name) {
    this.name = name;
}

function Roland(name) {
    this.name = name;
}

Roland.prototype = new Synthesizer();

Synth1 = new Synthesizer("Analogue");
Synth2 = new Roland("Fantom G6");

document.write(Synth1.name + '<br>');
document.write(Synth2.name + '<br>');

You can do this by several way. 你可以通过几种方式做到这一点。

For example : 例如 :

var Synthesizer = function(name){
   this.name = name;
}

function Roland(name) {
   Synthesizer.call(this, name); // you call the constructor of Synthesizer 
                                 // and force Synthesizer's this to be Roland's this
}
function clone(obj){
   var ret = {};
   for(var i in obj){ ret[i] = obj[i]; }
   return ret;
}
Roland.prototype = clone(Synthesizer.prototype); // inheritance of public functions

For Function.prototype.call : https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function/Call 对于Function.prototype.call: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function/Call

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

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