使用原型的JavaScript继承

使用原型的JavaScript继承,javascript,prototypal-inheritance,Javascript,Prototypal Inheritance,我已经编程20多年了,但最近转向了JavaScript。尽管花了数小时在网络上搜寻,但原型继承方法的成本还没有下降 在下面的简化代码中,我试图将'name'属性从合成器'class'继承到罗兰'class',但我似乎能够访问它的唯一方法是使用'Synth2.prototype.name'而不是'Synth2.name'(返回未定义)。我想让这种方法发挥作用,这样我就可以使用“Synth2.name”,因为可移植性是一项设计要求 我将非常感谢任何帮助 function Synthesizer(na

我已经编程20多年了,但最近转向了JavaScript。尽管花了数小时在网络上搜寻,但原型继承方法的成本还没有下降

在下面的简化代码中,我试图将'name'属性从合成器'class'继承到罗兰'class',但我似乎能够访问它的唯一方法是使用'Synth2.prototype.name'而不是'Synth2.name'(返回未定义)。我想让这种方法发挥作用,这样我就可以使用“Synth2.name”,因为可移植性是一项设计要求

我将非常感谢任何帮助

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>');
函数合成器(名称){
this.name=名称;
}
罗兰函数(名称){
this.prototype=新合成器(名称);
}
Synth1=新合成器(“模拟”);
Synth2=新罗兰(“Fantom G6”);
document.write(Synth1.name+“
”); document.write(Synth2.name+“
”);
谢谢大家!(现在通过调用超级类进行更新)

函数合成器(名称){
this.name=名称;
this.rendersound=函数(){
文件。写下(“将信封应用到“+this.name+”
”; } } 罗兰函数(名称){ 合成器。调用(此,名称); 这个原型=合成器; this.Synthesizer_rendersound=this.rendersound; this.rendersound=函数(){ 编写(“将差分插值应用于“+this.name+”
”); 这个合成器渲染声音(这个); } } Synth1=新合成器(“模拟”); Synth2=新罗兰(“Fantom G6”); document.write(Synth1.name+“
”); document.write(Synth2.name+“
”); 文件。写(“
”); Synth1.rendersound(); 文件。写(“
”); Synth2.rendersound(); 文件。写(“
”); document.write('Synth1.prototype'+Synth1.prototype+'
'); 编写('Synth2.prototype'+Synth2.prototype+'
'); 文件。写(“
”); 编写('Synth1.constructor'+Synth1.constructor+'
'); 编写('Synth2.constructor'+Synth2.constructor+'
');
我认为您必须设置构造函数的
原型,如下所示:

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>');
函数合成器(名称){
this.name=名称;
}
罗兰函数(名称){
this.name=名称;
}
Roland.prototype=新合成器();
Synth1=新合成器(“模拟”);
Synth2=新罗兰(“Fantom G6”);
document.write(Synth1.name+“
”); document.write(Synth2.name+“
”);
您可以通过多种方式完成此操作

例如:

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

对于Function.prototype.call:

我想它会变得更清晰。感谢您的回答,该链接提供了信息。您不需要将其更改为
var
,只需调用
call
。这两种方法都可以。@minitech是的。我在发布前做的一些测试中更改了它。使用
函数合成器(名称)
。。。当然也会有用的!谢谢你们,这正是我想要的!为了让其他尝试进行类似继承的人受益,我还设法实现了一种调用“Super”类函数的简单方法(请参阅更新原始帖子)
,您使用Roland创建的公共方法(例如:
Roland.prototype.foo=function(){}
)也将为合成器创建。这不是我们想要的。这就是为什么我们必须克隆Synthesizer.prototype:
Roland.prototype=clone(Synthesizer.prototype)更正确。谢谢您的回答。尽管为了将来可能移植到.NET/Java,我试图使继承发生在类中,而不是在类之外。@AndyC:直接放在类之后。把它当作一个关键词。谢谢!我认为JavaScript可能非常灵活。
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