Phewのコンストラクタ配列からアクセスされたコンストラクタの親の "静的"メソッドを実行します。ここで問題です:私は、「ゲーム」を持っている以上、このように、アクションの配列からランダムなアクションを選択する必要がランダムシミュレータのように:Javascript
actions = [ Action1, Action2, Action3 ]
私は継承クラスとして書かれたアクションを持っていますアクション親クラス:私はアクションを実行し、その結果を得るためにやってるだから何
function Action() {
this.targets = [];
this.used = [];
this.execute = function(player) {
doStuff();
return whatever;
};
}
//btw the below I've seen in a JS OOP tutorial but it doesn't work and I have to implement init() in every child action
Action.init = function(player) {
var a = new this.constructor();
return a.execute(player);
};
Action.checkRequirements = function() {
return true;
};
Action1.prototype = new Action();
Action1.prototype.constructor = Action1;
function Action1 {
this.execute = function(player) {
doStuff();
return whatever;
}
}
Action1.init = function(player) {
var a = new Action1();
return a.execute(player);
}
は、var foo = actions.getRandomVal().init();
はそれがうまく機能(getRandomValは、配列からランダムな値を返す単純なカスタムスクリプトである)であるオブジェクトを作成しますインスタンスは、すべてのプロパティとメソッドを適切に継承し、exec()メソッドを実行し、その結果を返します...しかし、今ではcheckRequirements()
私がやりたい100 +アクションの10%のように実装したいメソッドです。単純にActionクラスから継承して、子クラスに実装されていない場合はtrueを返すだけですどのように考えがありません。私がvar a = actions.getRandomVal();
を実行した後にa.checkRequirements();
を実行すると、a.checkRequirementsが関数ではないという例外がスローされます。
PS:これは、(大規模な)友人グループのための比較的小さな非営利プロジェクトです。すべてのブラウザで動作する必要はありません。Chromeで動作する必要があります。それのためのクロム。
'Action1.prototype =新しいアクション();' [あなたのOOPが動作しない]理由の一つです(https://stackoverflow.com/questions/12592913/what-is-the-ここで新しいキーワードを使用する理由) – Bergi