Skip to content

Instantly share code, notes, and snippets.

@nukisashineko
Created August 23, 2017 22:04
Show Gist options
  • Save nukisashineko/2d6e22c89b3e4218bc47a6853da030e8 to your computer and use it in GitHub Desktop.
Save nukisashineko/2d6e22c89b3e4218bc47a6853da030e8 to your computer and use it in GitHub Desktop.
const Bot = function () {
this.memberVariable = 'aaa';
};
Bot.prototype.doSomething = function () {
console.log(this.memberVariable);
};
Bot.prototype.a = async function () {
// this.doSomething(); // => this.doSomething is not a function
console.log(this); // => { a_func: [Function], b_func: [Function], c_func: [Function] }
};
Bot.prototype.b = function () {
};
Bot.prototype.c = async function () {
};
Bot.prototype.process = async function (text) {
// textによって呼び出す関数を分ける。
const t = this;
const handle = {
a_func: t.a,
b_func: t.b,
c_func: t.c,
};
if (handle[text]) {
await handle[text]();
}
};
const bot = new Bot();
bot.process('a_func');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment