【发布时间】:2015-04-24 06:55:33
【问题描述】:
问题:
一个看似简单的问题,我在过去 2 周里断断续续地研究(请放轻松,因为我是新手!):
在使用 Require.js 和 Revealing Module Pattern 时,如何在 JavaScript 中巧妙地实现继承?
示例:
这是一个示例模块,它是某种类型的“Component”的基类:
define('Component', [], function () {
"use strict";
var _privateVar = 10;
var _doPrivateThings = function () { /* do stuff */ };
var init = function () { /* do stuff */ };
var update = function () { /* do stuff */ };
return {
init : init,
update : update
};
});
接下来我要实现 CakeComponent,它应该继承 Component 的所有内容,并允许我编辑/添加方法和属性:
define('CakeComponent', ['Component'], function (Component) {
"use strict";
// Setup inheritance
var CakeComponent = function() {}
CakeComponent.prototype = new Component();
// Add/edit methods/properties
CakeComponent.prototype.newMethod = function () { /* do stuff */ };
return {
init : CakeComponent.init,
update : CakeComponent.update,
newMethod : CakeComponent.newMethod
};
});
首先,我不确定这是否完全有意义,但其次,我的 CakeComponent 感觉有点恶心,因为现在我到处都有这个 CakeComponent 冗余,我不得不“重新显示”@ 987654329@ 和 update 方法。
我真的更喜欢这样的东西(我意识到这没有意义,它实际上只是伪代码):
define('CakeComponent', ['Component'], function (Component) {
"use strict";
this.extends(Component);
var newMethod = function () { /* do stuff */ };
return {
newMethod : newMethod
};
});
任何提示或建议将不胜感激。谢谢。
更多详情
- 也许我应该总是在
define包装器中创建一个类对象?我见过有人这样做,但在我遇到这个问题之前似乎没有必要。 - 函数对象上的
.call()方法在这种情况下是否有用?例如使用Component.call() - @Bergi 请见下文:
define([], function () {
"use strict";
var Component = function () {
var _privateVar = 10;
var _doPrivateThings = function () { /* do stuff */ };
this.init = function () { /* do stuff */ };
this.update = function () { /* do stuff */ };
};
return Component;
});
【问题讨论】:
-
new Component();似乎根本不起作用,您的Component是模块对象而不是类构造函数? -
@Bergi 是的,我以前读过,谢谢,但我的问题实际上是 requirejs,而不仅仅是 RVP 的继承或受保护的变量。嗯,我会看看为什么它不起作用,谢谢
-
你真的想要类(可多次实例化)还是模块对象(单例)?在第一种情况下,您需要
return来自您的definer函数的构造函数,而不是您当前所做的对象文字。如果你想拥有模块,你所说的“继承”是什么意思? -
是的,就是这样。现在,您可以通过将原型用于不需要特权的方法来扩展该模式,您可以inherit from the "class" as always
标签: javascript inheritance requirejs revealing-module-pattern