2011-11-20 77 views
0

我已经编程了20多年,但最近转向使用JavaScript。尽管花费了数小时的时间来拖网,但这个便士还没有用原型继承方法。使用原型进行JavaScript继承

在下面的简化代码中,我试图从Synthesizer'class'继承'roland'类的'name'属性,但我似乎能够访问它的唯一方法是使用'Synth2 .prototype.name'而不是'Synth2.name'(它返回undefined)。我希望得到这种方法,以便我可以使用'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>'); 

谢谢你们! (现在与调用父类更新)...

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>'); 
+0

退房http://javascript.crockford.com/prototypal.html我认为它会变得更加清晰。 – PM5544

+0

感谢您的回答,链接内容丰富。 – AndyC

回答

1

您可以通过几种方式做到这一点。

例如:

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:https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function/Call

+0

你不需要把它改成'var'只是为了调用'call'就可以了。它将以任何方式工作。 – Ryan

+0

@minitech是的。我在发布之前进行的一些测试中对其进行了更改。使用'函数合成器(名称)'...也将工作,当然! – fflorent

+0

谢谢你们,这正是我一直在寻找的!对于任何尝试进行类似继承的人来说,我也设法实现了一种调用“超级”类功能的简单方法(请参阅更新到原始文章)。 – AndyC

1

我相信你有设置构造函数的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>'); 
+0

感谢您的回答。虽然我试图让继承发生在类中,而不是在它之外,以便将来可能移植到.NET/Java。 – AndyC

+0

@AndyC:直接放在课后。把它看作是一个关键字。 – Ryan

+0

谢谢!我认为JavaScript可能过于灵活。 – AndyC