【问题标题】:Forward parameters to another method将参数转发到另一个方法
【发布时间】:2016-09-14 21:30:38
【问题描述】:

来自 jQuery 的方法 on 有不同的签名:

# Signature 1
$('.container').on 'click', -> ...
# Signature 2
$('.container').on 'click', 'button', -> ...

我正在类中构建一个方法,我想使用正确的签名将其参数转发给on 方法

obj = new MyObject $('.container')

# Uses signature 1
obj.on 'mouseenter', -> ...
# Uses signature 2
obj.on 'mouseleave', 'button', -> ...

到目前为止,我构建的仅使用第一个签名。

class MyObject
    constructor: (@el) ->
    on: (event, callbacks) => @el.on event, callbacks

如何修改我的类,使其可以同时使用这两个签名?

【问题讨论】:

    标签: jquery coffeescript


    【解决方案1】:

    您可以通过使用“Splats”来实现:

    class MyObject
        constructor: (@el) ->
        on: (events..., last) => @el.on event, last for event in events
    

    这种方法实际上将同时涵盖两种情况。

    【讨论】:

      猜你喜欢
      • 2017-06-24
      • 1970-01-01
      • 1970-01-01
      • 2012-11-28
      • 2011-04-24
      • 1970-01-01
      • 1970-01-01
      • 2016-12-21
      • 1970-01-01
      相关资源
      最近更新 更多