如何避免 CoffeeScript 方法变量被包装在对象文字中?
CoffeeScript 将方法内声明的变量包装在对象文字中。
所以,这样:
@Templates =
get: (templateName) ->
result: '' # DECLARED HERE
$.ajax(
'Views/Templates/' + templateName + '.html',
type: 'GET'
dataType: 'html'
success: (data) ->
result = data # ASSIGNED HERE
async: false
)
return result # RETURNED HERE
变成这样:
(function() {
this.Templates = {
get: function(templateName) {
({
result: '' //DECLARED IN AN OBJECT LITERAL - I DON'T WANT THIS
});
$.ajax('Views/Templates/' + templateName + '.html', {
type: 'GET',
dataType: 'html',
success: function(data) {
var result; //DECLARED LOCAL TO THE CALLBACK - I DON'T WANT THIS
return result = data;
},
async: false
});
return result; //RETURNED HERE - UNASSIGNED
}
};
}).call(this);
但我需要的,对我有用的是:
(function() {
this.Templates = {
get: function(templateName) {
var result = '' //DECLARED HERE
$.ajax('Views/Templates/' + templateName + '.html', {
type: 'GET',
dataType: 'html',
success: function(data) {
return result = data; //ASSIGNED HERE
},
async: false
});
return result; //RETURNED HERE
}
};
}).call(this);
我做错了什么?我该如何解决这个问题?
CoffeeScript wraps variables declared inside methods in an object literal.
So, this:
@Templates =
get: (templateName) ->
result: '' # DECLARED HERE
$.ajax(
'Views/Templates/' + templateName + '.html',
type: 'GET'
dataType: 'html'
success: (data) ->
result = data # ASSIGNED HERE
async: false
)
return result # RETURNED HERE
Becomes this:
(function() {
this.Templates = {
get: function(templateName) {
({
result: '' //DECLARED IN AN OBJECT LITERAL - I DON'T WANT THIS
});
$.ajax('Views/Templates/' + templateName + '.html', {
type: 'GET',
dataType: 'html',
success: function(data) {
var result; //DECLARED LOCAL TO THE CALLBACK - I DON'T WANT THIS
return result = data;
},
async: false
});
return result; //RETURNED HERE - UNASSIGNED
}
};
}).call(this);
But what I need, and that works for me, is this:
(function() {
this.Templates = {
get: function(templateName) {
var result = '' //DECLARED HERE
$.ajax('Views/Templates/' + templateName + '.html', {
type: 'GET',
dataType: 'html',
success: function(data) {
return result = data; //ASSIGNED HERE
},
async: false
});
return result; //RETURNED HERE
}
};
}).call(this);
What am I doing wrong? How can I fix this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你所说的闭包并不是闭包(JavaScript 中的闭包始终是函数)。它是一个括在括号中的对象文字。
我对 CoffeeScript 不太熟悉,但是如果您希望
result
成为get
函数中的局部变量,我相信您想要更改为
前者是对象初始值设定项格式(因此它被转换为对象文字),后者是变量赋值。似乎在 CoffeeScript 网站的词法范围下有所介绍。
What you've called a closure isn't a closure (closures in JavaScript are always functions). It's an object literal wrapped in parentheses.
I'm not really au fait with CoffeeScript, but if you want
result
to be a local variable in theget
function, I believe you want to changeto
The former is the object initializer format (hence it getting translated into an object literal), the latter is a variable assignment. Seems to be covered on the CoffeeScript site under lexical scoping.
通过
result: ''
您可以声明对象的属性。您需要的是通过result = ''
声明一个局部变量:By
result: ''
you declare a property of an object. What you needed was to declare a local variable byresult = ''
:为什么要使用
:
?它没有被包装在闭包中,您只是定义了一个匿名对象表达式。只需这样做:
也就是说,为什么要执行同步 GET?这是非常不受欢迎的,并且会使您的网站响应速度降低。
Why are you using a
:
? It's not being wrapped in a closure, you are just defining an anonymous object expression.Just do this:
That said, why are you doing a synchronous GET? That's very frowned upon and will make your site must less responsive.