2017-01-31 4 views
1

私はDRYプログラミングに従おうとしています。私は自分自身を繰り返していますので、親メソッド内でいくつかのコードを助けるメソッドをネストしようとしました。何かを持ち帰るなかったclass.chat.method()を呼び出す を期待通りにES6クラスのネストメソッド?

chat() { 
    client.on("chat", (channel, user, message, self) => { 

    method() { 
    // code here 
    } 

    method() { 
    // code here 
    { 

    } 
} 

しかし、それがうまくいきませんでした。私が本当に必要とするのは、私が使用するすべての方法でclient.on("chat", callback())と呼ぶ私のDRYプログラミングを削除することです。これを防ぐことができ、その中で呼び出されたメソッドを持つスニペットが1つだけあるかどうか不思議です。

FULL CODE:

watchFor(command, res, sendersName, prefix) { 
    this.client.on("chat", (channel, user, message, self) => { 
     console.log(this._showSendersName.whitelistedCommands); 
     if (message == this.prefix + command || message == prefix + command) { 
      return this.client.say(channel, res); 
     } 
    }); 
} 
modOnly(command, res) { 
    this.client.on("chat", (channel, user, message, self) => { 
     if (this._showSendersName == false) { 
      if (self) return 
     } 
     if (message == this.modPrefix + command && user.mod || message == this.prefix + command && user.mod) { 
      return this.client.say(channel, res); 
     } 
    }); 
} 
broadcasterOnly(command, res) { 
    this.client.on("chat", (channel, user, message, self) => { 
      if (this._showSendersName == false) { 
       if (self) return 
      } 
      if (message == this.prefix + command && user.badges.broadcaster == 1) { 
       return this.client.say(channel, res); 
      } 
    }); 
} 
+0

イベントを扱う 'client'ではなく、全く別のオブジェクトにそれらを配置したいのですか? – Bergi

+0

あなたはどういう意味ですか? –

+0

私はチャットをイベントに対処したいですが、チャットイベントを呼び出す代わりにただ1つのチャットイベントを1回だけ、クラスに新しいメソッドを書きます。 –

答えて

1

あなたは、オブジェクト初期化子の外ES6メソッド定義の省略形を使用することはできません。関数スコープ内で別の関数を宣言してみてください:

+0

これは技術的に私が探しているものですが、それでも私はそれを繰り返していますか? app.jsでbot.chat()を呼び出す必要があります。sharedMethod()は可能ですか? –