CoffeeScript always returns in anonymous function(CoffeeScript 总是以匿名函数返回)
问题描述
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 总是以匿名函数返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:CoffeeScript 总是以匿名函数返回


- 在不使用循环的情况下查找数字数组中的一项 2022-01-01
- 我不能使用 json 使用 react 向我的 web api 发出 Post 请求 2022-01-01
- 如何向 ipc 渲染器发送添加回调 2022-01-01
- 从原点悬停时触发 translateY() 2022-01-01
- 使用 iframe URL 的 jQuery UI 对话框 2022-01-01
- 如何调试 CSS/Javascript 悬停问题 2022-01-01
- 为什么悬停在委托事件处理程序中不起作用? 2022-01-01
- 为什么我的页面无法在 Github 上加载? 2022-01-01
- 如何显示带有换行符的文本标签? 2022-01-01
- 是否可以将标志传递给 Gulp 以使其以不同的方式 2022-01-01