Skip to content

Instantly share code, notes, and snippets.

@triacontane
Last active October 21, 2019 04:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save triacontane/3cc27df000bea0b611e7309b2f8145d4 to your computer and use it in GitHub Desktop.
Save triacontane/3cc27df000bea0b611e7309b2f8145d4 to your computer and use it in GitHub Desktop.
ifやswitchを使わずに複数のプラグインコマンドをシンプルに定義するサンプル
(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) {
};
})();
@triacontane
Copy link
Author

実行例
◆プラグインコマンド:COMMAND1 aaa

@triacontane
Copy link
Author

ES5準拠

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment