Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript CoffeeScript始终以匿名函数的形式返回_Javascript_Coffeescript - Fatal编程技术网

Javascript CoffeeScript始终以匿名函数的形式返回

Javascript CoffeeScript始终以匿名函数的形式返回,javascript,coffeescript,Javascript,Coffeescript,我正在尝试编写一些CoffeScript函数,它在选中表中的复选框时检查表中的所有复选框 我在CoffeeScript中的函数如下所示: $("table.tableview th input:checkbox").live 'click', -> checkedStatus = this.checked $("table.tableview tbody tr td:first-child input:checkbox").each -&g

我正在尝试编写一些CoffeScript函数,它在选中表中的复选框时检查表中的所有复选框

我在CoffeeScript中的函数如下所示:

$("table.tableview th input:checkbox").live 'click', -> 
  checkedStatus = this.checked
  $("table.tableview tbody tr td:first-child input:checkbox").each ->
      this.checked = checkedStatus
$("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;
  });
});
$("table.tableview tbody tr td:first-child input:checkbox").each ->
    this.checked = checkedStatus
true
它非常适合检查所有的框。但是,当取消选中时,它不起作用。编译后的JS如下所示:

$("table.tableview th input:checkbox").live 'click', -> 
  checkedStatus = this.checked
  $("table.tableview tbody tr td:first-child input:checkbox").each ->
      this.checked = checkedStatus
$("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;
  });
});
$("table.tableview tbody tr td:first-child input:checkbox").each ->
    this.checked = checkedStatus
true
它不起作用,因为在第一个设置为false之后,函数的返回将为false。但是,我不知道如何抑制coffee脚本的默认返回行为。请帮忙

当我根据Flambino的建议添加一个“true”时,我得到以下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;
});
我可以在函数中获取return语句的唯一方法是将其全部放在如下位置:

$("table.tableview th input:checkbox").live 'click', -> 
  checkedStatus = this.checked
  $("table.tableview tbody tr td:first-child input:checkbox").each ->
      this.checked = checkedStatus
$("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;
  });
});
$("table.tableview tbody tr td:first-child input:checkbox").each ->
    this.checked = checkedStatus
true

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

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

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

这与在javascript中那样的函数中编写
返回true
没有区别。只需使用空格而不是
{}
来生成代码块。

如果只使用
return
(或者,等价地,
undefined
)作为函数的最后一行,CoffeeScript编译器将为您提供根本没有
return
的JS。因此,编写代码最有效的方法是

$("table.tableview th input:checkbox").live 'click', -> 
  checkedStatus = this.checked
  $("table.tableview tbody tr td:first-child input:checkbox").each ->
    this.checked = checkedStatus
    return
  return
(当然,您可以不使用第二个
返回值
。只有
false
的返回值在jQuery中有效。)


还有一种建议的语法(
-/>
)用于定义没有返回值的函数;请参阅。

是的。看起来没有办法抑制咖啡的回归行为@弗拉米诺我明白得那么远。我的问题基本上已经被你的第一篇帖子回答了;)谢谢。然而,我确实有一个问题,如果我使用了您指定的缩进,我最终会在错误的行中返回true语句。结果是,我的文件中出现了一些制表符与空格的问题,这破坏了CoffeeScript的“重要空白”范围。很高兴我知道了。第二次是Thxhelp@Gidogeek:啊,是的,注意制表符和软制表符(空格)。通常,1个实制表符的宽度为2-4个空格,但它仍然只计为1个空格字符。如果您可以在文本编辑器中选择“显示不可见项”,这将是一个很大的帮助,因为它可以使选项卡清晰可见。