2017-01-31 1 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())라고 부르는 내 드라이 프로그램을 제거하는 것입니다. 호기심이 막을 수 있는지 그리고 그 안에 호출 된 메소드가있는 단편이 하나 있습니다.

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

이벤트를 처리하는 '클라이언트'가 아닌 다른 객체에 넣으려는 것 같습니까? – Bergi

+0

음, 정확히 무엇을 의미합니까? –

+0

나는 이벤트를 다루기 위해 채팅을 원한다. 그러나 클래스에 새로운 메소드를 쓸 때마다 매번 채팅 이벤트를 호출하는 대신 하나의 채팅 이벤트 만 필요하다. –

답변

1

당신은 개체 이니셜 라이저의 외부 ES6 방법 정의 속기를 사용할 수 없습니다. 함수 범위 내에서 다른 함수를 한 번 선언 해보십시오.

+0

이제 기술적으로 내가 찾고있는 것은 무엇입니까? app.js에서 bot.chat()을 호출해야합니다. sharedMethod()가 가능합니까? –

관련 문제