CoffeeScript 中的匿名函数语法

发布于 2024-10-02 07:22:29 字数 497 浏览 3 评论 0原文

我一直在查看 CoffeeScript ,但我不明白您将如何编写这样的代码。它如何处理语法中的嵌套匿名函数?

;(function($) {
          var app = $.sammy(function() {

            this.get('#/', function() {
              $('#main').text('');
            });

            this.get('#/test', function() {
              $('#main').text('Hello World');
            });

          });

          $(function() {
            app.run()
          });
        })(jQuery);

I've been looking at CoffeeScript and I'm not understanding how you would write code like this. How does it handle nested anonymous functions in its syntax?

;(function($) {
          var app = $.sammy(function() {

            this.get('#/', function() {
              $('#main').text('');
            });

            this.get('#/test', function() {
              $('#main').text('Hello World');
            });

          });

          $(function() {
            app.run()
          });
        })(jQuery);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

携余温的黄昏 2024-10-09 07:22:29

实际上并没有编译它,但这应该可以工作

(($) ->
  app = $.sammy ->

    this.get '#/', ->
      $('#main').text '' 

    this.get '#/test', ->
      $('#main').text 'Hello World'

  $(->
    app.run()
  )
)(jQuery);

didn't actually compile it, but this should work

(($) ->
  app = $.sammy ->

    this.get '#/', ->
      $('#main').text '' 

    this.get '#/test', ->
      $('#main').text 'Hello World'

  $(->
    app.run()
  )
)(jQuery);
入画浅相思 2024-10-09 07:22:29

Matt 的答案是正确的,但这里有一个替代方法:

在 CoffeeScript 1.0(在提出这个问题几周后发布)中,引入了一个 do 运算符,它运行紧随其后的函数。它主要用于捕获循环中的变量,因为

for x in arr
  do (x) ->
    setTimeout (-> console.log x), 50

(将对 x 的引用传递到匿名函数中)的行为与后者不同,

for x in arr
  setTimeout (-> console.log x), 50

后者只会重复输出 arr 中的最后一个条目,因为只有一个 x

无论如何,您应该意识到 do 作为一种运行匿名函数而无需额外括号的方式,尽管它在参数传递方面的功能目前有点有限。我提出了扩大范围的提案

目前,您的代码示例的等效项是

do ->
  $ = jQuery
  ...

如果我的提案被接受,则可以

do ($ = jQuery) ->
  ...

改为编写。

Matt's answer is correct, but here's an alternative method:

In CoffeeScript 1.0 (released a few weeks after this question was posed), a do operator was introduced that runs the function that immediately follows it. It's mostly used for capturing variables in loops, since

for x in arr
  do (x) ->
    setTimeout (-> console.log x), 50

(which passes a reference to x into the anonymous function) behaves differently than

for x in arr
  setTimeout (-> console.log x), 50

The latter will simply output the last entry in arr repeatedly, since there's only one x.

Anyway, you should be aware of do as a way of running an anonymous function without the extra parentheses, though its capabilities with respect to argument-passing are a bit limited at the moment. I've raised a proposal to broaden them.

Currently, the equivalent of your code example would be

do ->
  $ = jQuery
  ...

If my proposal is accepted, it will be possible to write

do ($ = jQuery) ->
  ...

instead.

溺ぐ爱和你が 2024-10-09 07:22:29

短款

do ($=jQuery)->
 app = $.sammy ->
   @get '#/', -> $("#main").text ''
   @get '#/test', -> $('#main').text 'Hello world'
 $ -> app.run()

Short variant

do ($=jQuery)->
 app = $.sammy ->
   @get '#/', -> $("#main").text ''
   @get '#/test', -> $('#main').text 'Hello world'
 $ -> app.run()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文