CoffeeScript 总是以匿名函数返回
我正在尝试编写一些 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
它非常适合选中所有框。但是,取消选中后,它不起作用。编译后的 JS 看起来像这样:
$("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;
});
});
它不起作用,因为在第一个设置为 false 后,函数的返回将为 false。然而,我不知道如何抑制咖啡脚本的这种默认返回行为。请帮忙。
当我按照 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 tbody tr td:first-child input:checkbox").each ->
this.checked = checkedStatus
true
我做错了什么?感谢到目前为止的帮助
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您仅使用
return
(或等效地,undefined
)作为函数的最后一行,CoffeeScript 编译器将为您提供没有return
的 JS > 完全没有。因此,编写代码的最有效方法是(当然,您可以安全地执行没有第二个
return
的操作。只有false
返回值在 jQuery 中才有效。 )还有一个建议的语法 (
-/>
) 用于定义没有返回值的函数;请参阅问题 899。If you just use
return
(or, equivalently,undefined
) as the last line of a function, the CoffeeScript compiler will give you JS with noreturn
at all. So the most efficient way of writing your code would be(You can safely do without the second
return
, of course. Only a return value offalse
has an effect in jQuery.)There was also a proposed syntax (
-/>
) for defining a function with no return value; see issue 899.只需添加一个
true
作为函数的最后一行,coffeescript 就会编译 JS 来返回它:换句话说,CoffeeScript 总是返回最后一行的结果(就像 Ruby 一样)
编辑(问题更新后):同样,您无法阻止 CoffeeScript 返回函数中最后一行的值 - CoffeeScript 的部分要点是它正是这样做的。
CoffeeScript 有大量的空格,因此缩进表示属于一起的内容 - 您的示例实际上是正确的:
这与在函数中编写
return true
没有区别,就像在 javascript 中一样。您只需使用空格而不是{}
来创建代码块。Just add a
true
as the last line of your function, and coffeescript will compile the JS to return that instead: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:
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.