【问题标题】:How to write literal jQuery object in coffeescript如何在咖啡脚本中编写文字 jQuery 对象
【发布时间】:2012-05-21 05:51:56
【问题描述】:

我正在尝试将一些代码转换为咖啡脚本,但遇到了问题:

var $el = $('<span/>', {
    class : 'myclass',
    click : function () {
        var $this = $(this)
        if (foo) { // radio & check
            baz($this)
        }else{
            bla($this)
        }
    }
});

我在咖啡里是这样写的:

$el = $('<span/>',
  class: 'myclass'
  click: ->
    $this = $(this)
    if foo
      baz $this
    else
      bla $this
)

哪个工作得很好,但我真的不喜欢最后一个括号,有没有什么办法可以在没有括号的情况下编写这段代码,只是缩进?

【问题讨论】:

  • 我很想知道转向咖啡脚本背后的动机。可以分享给我吗?
  • 我希望大多数 javascript 开发人员不理解咖啡脚本这一事实将使其仅作为实验...:)

标签: javascript jquery coffeescript


【解决方案1】:

如果你去掉两个括号,效果很好。

$el = $ "<span/>",
  class: "myclass"
  click: ->
    $this = $(this)
    if foo
      baz $this
    else
      bla $this

另请参阅:http://js2coffee.org/

【讨论】:

    猜你喜欢
    • 2011-11-17
    • 2014-06-27
    • 1970-01-01
    • 1970-01-01
    • 2014-12-03
    • 2014-12-30
    • 2011-06-30
    • 2014-06-04
    • 2013-06-26
    相关资源
    最近更新 更多