17 javascript inheritance design-patterns module
我喜欢返回构造函数的模块模式,如下所述:http: //elegantcode.com/2011/02/15/basic-javascript-part-10-the-module-pattern/
但是我不确定如何从使用此模式实现的对象继承.假设我实现了一个父对象...
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;
}());
Run Code Online (Sandbox Code Playgroud)
我如何定义一个子对象,它也使用继承的模块模式,parent例如,我可以有选择地覆盖func2?
Ray*_*nos 18
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;
}());
Run Code Online (Sandbox Code Playgroud)