【问题标题】:How to bind all arguments of a event emitter如何绑定事件发射器的所有参数
【发布时间】:2021-08-11 12:24:21
【问题描述】:

有没有办法将所有 Client.on() 参数传递给函数? (Client 扩展 BaseClient 扩展 EventEmitter)例如:

Client.on("event", args => { function.call(args) })

我正在使用 TypeScript

【问题讨论】:

  • 就用Client.on("event", function)?
  • 你真的要使用.call()吗?

标签: javascript node.js typescript discord.js


【解决方案1】:

有几种方法可以做到这一点:

  • 使用...-运算符。

    Client.on("event", (...args) => {
      fx(...args);
    });
    
  • 使用调用或申请:

    Client.on("event", (...args) => {
      fx.apply(this, args);
    });
    
    Client.on("event", (...args) => {
      fx.call(this, ...args);
    });
    
  • 直接使用函数:

    Client.on("event", fx);
    

【讨论】:

    猜你喜欢
    • 2021-06-30
    • 2016-07-06
    • 2012-03-14
    • 2020-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-16
    相关资源
    最近更新 更多