为什么这个 CoffeeScript/JavaScript 不设置创建对象的属性?
我有以下两个 CoffeeScript 类定义。我期望他们有同样的行为,但他们没有。特别是在 DoesNotWork
实例上访问 A
是未定义的。
fields = ["A","B","C"]
class DoesNotWork
constructor: () ->
_.each(fields, (f) -> @[f] = ko.observable(''))
class DoesWork
constructor: () ->
@A = ko.observable('')
@B = ko.observable('')
@C = ko.observable('')
上面的代码编译为
var DoesNotWork, DoesWork, fields;
fields = ["A", "B", "C"];
DoesNotWork = (function() {
function DoesNotWork() {
_.each(fields, function(f) {
return this[f] = ko.observable('');
});
}
return DoesNotWork;
})();
DoesWork = (function() {
function DoesWork() {
this.A = ko.observable('');
this.B = ko.observable('');
this.C = ko.observable('');
}
return DoesWork;
})();
我到底错过了什么新手 JS?
I've the following two CoffeeScript class definitions. I expected them to the same behavior, but they don't. In particular accessing A
on instances of DoesNotWork
is undefined.
fields = ["A","B","C"]
class DoesNotWork
constructor: () ->
_.each(fields, (f) -> @[f] = ko.observable(''))
class DoesWork
constructor: () ->
@A = ko.observable('')
@B = ko.observable('')
@C = ko.observable('')
the above code compiles to
var DoesNotWork, DoesWork, fields;
fields = ["A", "B", "C"];
DoesNotWork = (function() {
function DoesNotWork() {
_.each(fields, function(f) {
return this[f] = ko.observable('');
});
}
return DoesNotWork;
})();
DoesWork = (function() {
function DoesWork() {
this.A = ko.observable('');
this.B = ko.observable('');
this.C = ko.observable('');
}
return DoesWork;
})();
What newbie JS subtly am I missing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
传递给 _.each 的匿名函数中的“this”绑定到匿名函数,而不是父对象。 正确绑定该对象
_.each 确实允许传递上下文对象,以便通过http://documentcloud.github .com/underscore/#each
因此,将引用传递给您尝试在每个的第三个参数中绑定到的对象:
'this' in the anonymous function passed to _.each is bound to the anonymous function, not the parent object. _.each does allow passing a context object so that this will be bound properly though
http://documentcloud.github.com/underscore/#each
so pass a ref to the object you are trying to bind to in the 3rd arg of each:
克雷格的答案是正确的,但另一种解决方案是将匿名函数定义为 绑定函数。在这种情况下,您可以编写
=>
将函数绑定到定义它的上下文,以便this
在函数中始终表示相同的内容 no不管它怎么称呼。对于回调来说,这是一种非常有用的技术,尽管在_.each
的情况下,它比传入this
的效率要低一些。您可以使用 Underscore 做同样的事情:但是
=>
可以节省您大量的打字时间!Craig's answer is correct, but an alternative solution is to define your anonymous function as a bound function. In this case, that would let you write
The
=>
binds the function to the context in which it's defined, so thatthis
always means the same thing in the function no matter how it's called. It's a very useful technique for callbacks, though in the case of_.each
, it's a bit less efficient than passingthis
in.You could do the same thing using Underscore by writing
but
=>
saves you a lot of typing!另一种解决方案(可以说是最具可读性和最高效的)是跳过
_.each
并使用 CoffeeScript 的for...in
迭代:您甚至可以对循环进行后缀处理以使这是一句简单的话:
请记住,CoffeeScript 中的循环不会创建上下文或影响范围;只有函数可以。
Yet another solution (arguably the most readable and efficient) is to skip
_.each
and instead use CoffeeScript'sfor...in
iteration:You could even postfix the loop to make it a one-liner:
Remember that loops in CoffeeScript don't create context or affect scope; only functions do.