通过 Javascript 原型继承重用代码
我对如何通过原型继承实现代码重用有点困惑。我正在按照 http://alexsexton.com/?p=51 创建的示例一个扬声器对象并将其与 jQuery 桥接。
假设我想要一个与示例中的扬声器类似的新扬声器,但现在带有额外的声音文件。我能想到的唯一代码是这样的:
var AnotherSpeaker = Object.create(Speaker);
$.extend(true, AnotherSpeaker, {
init: function(options, elem){
this.options.soundFile = options.soundFile || this.options.soundFile;
Speaker.init.call(this, options, elem);
},
options:{
soundFile: 'abc.wav'
},
_playSound: function(){
//....code to play the sound this.options.soundFile;
},
speak: function(msg){
this._playSound();
Speaker.speak.call(this, msg);
}
});
$.plugin('AnotherSpeaker', AnotherSpeaker); //jquery plugin bridge
但这种方法实际上对我来说听起来相当“经典”。我通过 Speaker.xxx.call
调用“超级”。我想我应该做差异继承,但不知道如何做?有什么帮助吗?
I'm a bit confused with how to achieve code re-use with the prototypal inheritance. I'm following the example at http://alexsexton.com/?p=51 where it create a Speaker object and bridge it with jQuery.
Say I want a new speaker that is similar to the one in example, but now with a extra sound file. The only code I can think of is like:
var AnotherSpeaker = Object.create(Speaker);
$.extend(true, AnotherSpeaker, {
init: function(options, elem){
this.options.soundFile = options.soundFile || this.options.soundFile;
Speaker.init.call(this, options, elem);
},
options:{
soundFile: 'abc.wav'
},
_playSound: function(){
//....code to play the sound this.options.soundFile;
},
speak: function(msg){
this._playSound();
Speaker.speak.call(this, msg);
}
});
$.plugin('AnotherSpeaker', AnotherSpeaker); //jquery plugin bridge
But this approach actually sounds quite 'classical' to me. I'm calling the 'super' through Speaker.xxx.call
. I think I'm suppose to do differential inheritance but can't figure out how? Any help?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您已经在进行差异继承(指定
AnotherSpeaker
与Speaker
的不同之处)。关于通过
call
对Speaker.speak
进行“超级调用”的问题:是的,这很痛苦。超级调用是 JavaScript 无法提供太多开箱即用的帮助的领域。根据您正在使用的结构,这可能是最好的方法。几年前我定义了一个系统超级调用既简单又高效。它涉及使用辅助函数来创建构造函数(我在文章中将它们称为类,这是我当时还没有在脑海中进行类到原型转换的产物),但除了术语之外,它是实际上是原型继承。您可能会发现这很有帮助。
You're already doing differential inheritance (specifying what's different about
AnotherSpeaker
as opposed toSpeaker
).Regarding your issue with the "supercall" to
Speaker.speak
viacall
: Yes, that is a pain. Supercalls are an area where JavaScript doesn't help you very much out-of-the box. With the structure you're using, that's probably going to be the best way to do it.A couple of years ago I defined a system for making supercalls both easy and efficient. It involves using a helper function to create the constructor functions (I call them classes in the article, an artefact of my not having made the class-to-prototype transition in my head yet at the time), but aside from the terminology it is actually prototypical inheritance. You may find that helpful.
当面向对象刚刚兴起时,人们普遍认为继承有利于代码重用。新的观点是继承会产生高耦合,并且通过聚合而不是继承通常可以更好地实现重用。
我无法找到您链接到的文章,因此无法更具体地说明您的情况。
When object orientation was new, it was common to think that inheritance is good for code reuse. Newer wisdom is that inheritance creates high coupling, and that reuse is often better enabled with aggregation instead of inheritance.
I couldn't reach the article you linked to, so I can't be more specific to your case.