【发布时间】:2023-03-21 19:10:01
【问题描述】:
我正在尝试以这种方式使用模块模式实现继承:
Parent = function () {
//constructor
(function construct () {
console.log("Parent");
})();
// public functions
return this.prototype = {
test: function () {
console.log("test parent");
},
test2: function () {
console.log("test2 parent");
}
};
};
Child = function () {
// constructor
(function () {
console.log("Child");
Parent.call(this, arguments);
this.prototype = Object.create(Parent.prototype);
})();
// public functions
return this.prototype = {
test: function()
{
console.log("test Child");
}
}
};
但我不能从子实例调用test2()。
var c = new Child();
c.test2(); // c.test2 is not a function
我做错了什么?
【问题讨论】:
-
How to implement inheritance in JS Revealing prototype pattern? 深入解释了如何使用模块模式和继承。
标签: javascript inheritance module-pattern