Last active
October 21, 2019 04:44
-
-
Save triacontane/3cc27df000bea0b611e7309b2f8145d4 to your computer and use it in GitHub Desktop.
ifやswitchを使わずに複数のプラグインコマンドをシンプルに定義するサンプル
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function() { | |
'use strict'; | |
// コマンドとメソッド名とを関連付ける連想配列(オブジェクト) | |
var commandMap = { | |
COMMAND1:'testMethod', | |
COMMAND2:'testMethod2', | |
COMMAND3:'testMethod3' | |
}; | |
var _Game_Interpreter_pluginCommand = Game_Interpreter.prototype.pluginCommand; | |
Game_Interpreter.prototype.pluginCommand = function(command, args) { | |
_Game_Interpreter_pluginCommand.apply(this, arguments); | |
var methodName = commandMap[command]; | |
if (methodName) { | |
// ブラケット記法により呼び出すメソッド名を変数から指定できます。 | |
this[methodName](args); | |
} | |
}; | |
Game_Interpreter.prototype.testMethod = function(args) { | |
console.log(args[0]); | |
}; | |
Game_Interpreter.prototype.testMethod2 = function(args) { | |
}; | |
Game_Interpreter.prototype.testMethod3 = function(args) { | |
}; | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
実行例
◆プラグインコマンド:COMMAND1 aaa