gpt4 book ai didi

javascript - Coffeescript - 方法链与函数参数 [2] 或使用函数而不是参数

转载 作者:行者123 更新时间:2023-11-30 08:53:43 24 4
gpt4 key购买 nike

最近开始学习CoffeeScript,遇到了这样的问题。我想写 javascript :

TemplateManager.tmpl(this.template, this.modelJSON(), this.templateOptions()).done(
function(rendered) { // something1
}).fail(function(ex) {
// something2

});

我可以通过哪种方式获得它?我尝试重写:

TemplateManager.tmpl @template, @modelJSON(), @templateOptions()
.done (rendered) ->
#something1
.fail (ex) ->
#something2

我得到:

TemplateManager.tmpl(this.template, this.modelJSON(), this.templateOptions().done(function(rendered) {

}).fail(function(ex) {

}));

最佳答案

tmpldone 方法添加括号

TemplateManager.tmpl( @template, @modelJSON(), @templateOptions() )
.done( (rendered) ->
#something1
)
.fail (ex) ->
#something2

解决方案并不优雅,我认为其他人可能会在 coffeescript 中提供更好的方法

已更新

根据评论,删除done 的括号。我已经更新了代码,我认为这个代码很优雅

TemplateManager
.tmpl(@template, @modelJSON(), @templateOptions())
.done (rendered) ->
some
code
here

.fail (ex) ->
another
code
here

关于javascript - Coffeescript - 方法链与函数参数 [2] 或使用函数而不是参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15494410/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com