将继承与模块模式相结合 [英] Combining inheritance with the module pattern
本文介绍了将继承与模块模式相结合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我喜欢返回构造函数的模块模式,如:
http://elegantcode.com/2011/02/15/basic-javascript-part-10-the-module-pattern/
I like the module pattern that returns constructors as described in: http://elegantcode.com/2011/02/15/basic-javascript-part-10-the-module-pattern/
然而,我不知道如何从使用这种模式实现的对象继承。假设我有一个父对象实现了... ...
However I am not sure how to inherit from an object that is implemented with this pattern. Suppose I have a parent object implemented thus...
namespace('MINE');
MINE.parent = (function() {
// private funcs and vars here
// Public API - constructor
var Parent = function (coords) {
// ...do constructor stuff here
};
// Public API - prototype
Parent.prototype = {
constructor: Parent,
func1: function () { ... },
func2: function () { ... }
}
return Parent;
}());
如何定义也使用继承自 parent
,这样我可以有选择地覆盖,例如 func2
?
How do I define a child object that also uses the module pattern that inherits from parent
in such a way that I can selectively override, for example, func2
?
推荐答案
MINE.child = (function () {
var Child = function (coords) {
Parent.call(this, arguments);
}
Child.prototype = Object.create(Parent.prototype);
Child.prototype.constructor = Child;
Child.prototype.func2 = function () { ... };
return Child;
}());
这篇关于将继承与模块模式相结合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文