嵌套在ES6类的方法?

我一直在试图遵循一个DRY编程,我一直在重复自己,所以我试图嵌套方法,帮助一些代码的父方法。

chat() { client.on("chat", (channel, user, message, self) => { method() { // code here } method() { // code here { } } 

但是,这并没有按预期的方式调用class.chat.method()没有带回任何东西。 我真正需要帮助的是删除我的DRY编程,我打电话client.on("chat", callback())每一个我使用的方法。 好奇这是否可以防止,只有一个片段与其中调用的方法。

完整代码:

 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); } }); } 

在对象初始值设定项之外不能使用ES6方法定义。 尝试在函数范围内声明另一个函数:

 chat() { client.on("chat", (channel, user, message, self) => { const sharedMethod = () => { // code here } sharedMethod() }) }