CoffeeScript总是返回匿名函数 [英] CoffeeScript always returns in anonymous function

查看:125
本文介绍了CoffeeScript总是返回匿名函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我在CoffeeScript中的我的函数看起来像这样:。我试图写一些CoffeScript函数检查表中的所有复选框。

  $(table.tableview th input:checkbox)。live'click', - > 
checkedStatus = this.checked
$(table.tableview tbody tr td:first-child input:checkbox)。
this.checked = checkedStatus

它非常适合检查所有的框。但是,当取消选中它不工作。编译的JS看起来像这样:

  $(table.tableview th input:checkbox function(){
var checkedStatus;
checkedStatus = this.checked;
return $(table.tableview tbody tr td:first-child input:checkbox {
return this.checked = checkedStatus;
});
});

它不工作,因为在第一个设置为false后,函数的返回将是假。然而我没有线索如何抑制这种默认返回行为的咖啡脚本。请帮忙。



当我根据Flambino的建议添加true时,我得到以下JS

 code> $(table.tableview th input:checkbox)。live('click',function(){
var checkedStatus;
checkedStatus = this.checked;
$ (table.tableview tbody tr td:first-child input:checkbox)。每个(function(){
return this.checked = checkedStatus;
});
return true;
});

我可以在函数中获取return语句的唯一方法是:

  $(table.tableview tbody tr td:first-child input:checkbox)。 
this.checked = checkedStatus
true

我做错了什么?到目前为止的帮助

解决方案

只需添加 true 你的函数的最后一行,coffeescript将编译JS返回:

  $(table.tableview th input:复选框)。live'click', - > 
checkedStatus = this.checked
$(table.tableview tbody tr td:first-child input:checkbox)。
this.checked = checkedStatus
true

换句话说,CoffeeScript总是返回最后一行的结果(像Ruby一样)






编辑(问题更新后):



同样,你不能保持CoffeeScript不返回函数中最后一行的值 - CoffeeScript的一部分是它完全符合。



CoffeeScript有很多空白,所以缩进就是说什么属于一起 - 你的例子是正确的:

  $(table.tableview th input:checkbox)。live'click', - > 
checkedStatus = this.checked
$(table.tableview tbody tr td:first-child input:checkbox)。
this.checked = checkedStatus
true //使此函数(每个迭代器)返回true
true //使单击处理函数返回true

没有什么区别,只要在类似函数中写 return true 将在javascript中。您只需使用空白而不是 {} 来创建代码块。


I'm trying to write some CoffeScript function which checks all checkboxes in a table upon checking the checkbox in the th.

My function in CoffeeScript looks like this:

$("table.tableview th input:checkbox").live 'click', -> 
  checkedStatus = this.checked
  $("table.tableview tbody tr td:first-child input:checkbox").each ->
      this.checked = checkedStatus

It works great for checking all the boxes. However when unchecking it doesn't work. The compiled JS looks like this:

$("table.tableview th input:checkbox").live('click', function() {
  var checkedStatus;
  checkedStatus = this.checked;
  return $("table.tableview tbody tr td:first-child input:checkbox").each(function() {
    return this.checked = checkedStatus;
  });
});

It doesn't work because after the first one is set to false the return of the function will be false. I however have no clue how to suppress this default return behavior of coffee script. Please help.

When I add a "true" as per Flambino's suggestion I get the following JS

  $("table.tableview th input:checkbox").live('click', function() {
    var checkedStatus;
    checkedStatus = this.checked;
    $("table.tableview tbody tr td:first-child input:checkbox").each(function() {
      return this.checked = checkedStatus;
    });
    return true;
  });

The only way I can get the return statement inside the function is by putting it all the way like this:

    $("table.tableview tbody tr td:first-child input:checkbox").each ->
      this.checked = checkedStatus
                        true

What am I doing wrong ? Thx for the help so far

解决方案

Just add a true as the last line of your function, and coffeescript will compile the JS to return that instead:

$("table.tableview th input:checkbox").live 'click', -> 
    checkedStatus = this.checked
    $("table.tableview tbody tr td:first-child input:checkbox").each ->
        this.checked = checkedStatus
    true

In other words, CoffeeScript always returns the result of the last line (like Ruby does)


Edit (after the question was updated):

Again, you can't keep CoffeeScript from returning the value of the last line in a function - part of the point of CoffeeScript is that it does exactly that.

CoffeeScript has significant whitespace, so indentation is what says what belongs together - your example is is actually correct:

$("table.tableview th input:checkbox").live 'click', -> 
    checkedStatus = this.checked
    $("table.tableview tbody tr td:first-child input:checkbox").each ->
        this.checked = checkedStatus
        true // cause this function (the each-iterator) to return true
    true // causes the click handler function to return true

There's no difference between this and just writing return true in a function like you would in javascript. You just use whitespace instead of {} to make code blocks.

这篇关于CoffeeScript总是返回匿名函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆