Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

triacontane commented Oct 21, 2019

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

@triacontane
Copy link
Author

triacontane commented Oct 21, 2019

ES5準拠

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