【问题标题】:Coffeescript rewrite functionCoffeescript 重写功能
【发布时间】:2012-09-16 16:18:55
【问题描述】:

我有一个必须从 Coffeescript 生成的函数:

$("#user-tabs ul").idTabs({
    click: function(id, all, container, settings) {
        alert(id);
    } 
});

所以,我写了以下咖啡脚本:

$("#user-tabs ul").idTabs ->
    click: (id, all, container, settings) ->
       alert(id)
       return

但它不起作用。在输出 (.js) 我生成了以下代码:

$("#user-tabs ul").idTabs(function() {
  return {
    click: function(id, all, container, settings) {
      alert(id);
    }
  };
});

所以,click 函数是正确编写的,但它被一些“函数返回”闭包包裹。如何重写它以达到所需的代码(在最顶部)?有可能吗?

谢谢!

【问题讨论】:

  • -> 不是用于在 CoffeeScript 中定义函数吗?因此,您在第一行中定义了它。尝试改变它。

标签: javascript jquery coffeescript return anonymous-function


【解决方案1】:

只是缩进对象,不要让它成为一个函数(-> 所做的)。

$("#user-tabs ul").idTabs
    click: (id, all, container, settings) ->
       alert(id)
       return

See it.

【讨论】:

  • 我只能说:DOOOOOOH!就这么简单。傻我!太感谢了! :P
【解决方案2】:

删除 -> 其中定义一个函数(你只想调用它):

$("#user-tabs ul").idTabs 
    click: (id, all, container, settings) ->
       alert(id)
       return

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-06
    相关资源
    最近更新 更多