通过 Javascript 原型继承重用代码

发布于 2024-11-26 15:07:59 字数 909 浏览 0 评论 0原文

我对如何通过原型继承实现代码重用有点困惑。我正在按照 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一影成城 2024-12-03 15:07:59

您已经在进行差异继承(指定 AnotherSpeakerSpeaker 的不同之处)。

关于通过callSpeaker.speak进行“超级调用”的问题:是的,这很痛苦。超级调用是 JavaScript 无法提供太多开箱即用的帮助的领域。根据您正在使用的结构,这可能是最好的方法。

几年前我定义了一个系统超级调用既简单又高效。它涉及使用辅助函数来创建构造函数(我在文章中将它们称为类,这是我当时还没有在脑海中进行类到原型转换的产物),但除了术语之外,它是实际上是原型继承。您可能会发现这很有帮助。

You're already doing differential inheritance (specifying what's different about AnotherSpeaker as opposed to Speaker).

Regarding your issue with the "supercall" to Speaker.speak via call: 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.

自此以后,行同陌路 2024-12-03 15:07:59

当面向对象刚刚兴起时,人们普遍认为继承有利于代码重用。新的观点是继承会产生高耦合,并且通过聚合而不是继承通常可以更好地实现重用。

我无法找到您链接到的文章,因此无法更具体地说明您的情况。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文