【发布时间】:2018-05-16 23:44:03
【问题描述】:
我正在尝试使用 JavaScript 制作一个简单的游戏。我希望游戏中的每个级别都有稍微不同的行为。不过,我还想要一些默认行为,因为并非游戏的每个部分都会被更改。
我认为我应该尝试使用子类化和继承,也许使用这样的级别基础:
"use strict";
function LevelBase() {
this.load = function(level) {
if (level === 1) {
new Level1(this); // Can't do this = new Level1(this);
}
};
this.func = function() {
return 123;
};
}
function Level1(game) {
this.prototype = game;
this.func = function() {
return 456;
};
}
var game = new LevelBase();
game.load(1);
console.log(game.func()); // Should print 456
但是,这不起作用。它仍然使用默认行为,我感觉这是一种糟糕的方法,会使一切变得过于复杂。有没有一种工作方法可以做这样的事情?
任何帮助将不胜感激!
【问题讨论】:
标签: javascript inheritance subclass