为什么当 bug 出现时,方括号可以在 CoffeeScript 中工作
如果我想获得像这样从coffescript编译的js代码:
var sortableTodos = new Sortables("todo-list", {
constrain: true,
clone: true,
handle: ".todo-content",
onComplete: function(ele){
sortableTodos.serialize(false, function(element, index){
todo = Todos.get(element.getProperty("id").replace("todo-", ""));
todo.save({"order": index});
});
}
});
我无法编写如下所示的咖啡代码:
sortableTodos = new Sortables(
"todo-list"
(
constrain: true
handle: '.todo-content'
onComplete:(ele)->
sortableTodos.serialize false, (element,index)->
todo = Todos.get(element.getProperty("id")).replace("todo-","")
todo.save("order":index)
)
)
但是以下代码有效(它在onComplete之后有括号)
sortableTodos = new Sortables(
"todo-list"
(
constrain: true
handle: '.todo-content'
onComplete:((ele)->
sortableTodos.serialize false, (element,index)->
todo = Todos.get(element.getProperty("id")).replace("todo-","")
todo.save("order":index)
)
)
)
我不知道为什么?这是一个错误吗?
If i want to get a js code like this which compiles from coffeescript:
var sortableTodos = new Sortables("todo-list", {
constrain: true,
clone: true,
handle: ".todo-content",
onComplete: function(ele){
sortableTodos.serialize(false, function(element, index){
todo = Todos.get(element.getProperty("id").replace("todo-", ""));
todo.save({"order": index});
});
}
});
I can't write coffee code like below:
sortableTodos = new Sortables(
"todo-list"
(
constrain: true
handle: '.todo-content'
onComplete:(ele)->
sortableTodos.serialize false, (element,index)->
todo = Todos.get(element.getProperty("id")).replace("todo-","")
todo.save("order":index)
)
)
but the following works(it got brackets after onComplete)
sortableTodos = new Sortables(
"todo-list"
(
constrain: true
handle: '.todo-content'
onComplete:((ele)->
sortableTodos.serialize false, (element,index)->
todo = Todos.get(element.getProperty("id")).replace("todo-","")
todo.save("order":index)
)
)
)
I don't know why?Is it a bug?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
CoffeeScript 解析器在将函数参数拆分为多行时有许多怪癖。 (请参阅 issue 1135。)仅 YAML 允许函数的多行参数省略括号时的 -style 对象。因此,在
工作时(编译以将单个对象传递给
func
),其他参数通常需要与func
位于同一行。或者,您可以在行尾使用\
转义符(如在 JS 中),以使编译器将多行视为单行:针对您的情况的最佳解决方案是移动字符串到与函数调用相同的行,去掉对象文字周围的括号(如果愿意,可以使用大括号),并与缩进保持一致:
The CoffeeScript parser has many quirks when it comes to splitting function arguments across multiple lines. (See issue 1135.) Multi-line arguments to functions are only allowed for YAML-style objects when you omit parentheses. So while
works (compiling to pass a single object to
func
), other arguments often need to be on the same line asfunc
. Or you can use a\
escape at the end of the line, as in JS, to make multiple lines be treated by the compiler as a single line:The best fix for your case is to move your string to the same line as the function call, ditch the parentheses around the object literal (use curly braces if you like), and be consistent with your indentation: