gpt4 book ai didi

javascript - 将继承与模块模式相结合

转载 作者:可可西里 更新时间:2023-11-01 01:18:05 24 4
gpt4 key购买 nike

我喜欢返回构造函数的模块模式,如下所述: 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;
}());

我如何定义一个子对象,它也使用从 parent 继承的模块模式,以便我可以有选择地覆盖,例如 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;

}());

关于javascript - 将继承与模块模式相结合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8683125/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com