为什么这个 CoffeeScript/JavaScript 不设置创建对象的属性?

发布于 2024-10-27 04:28:15 字数 845 浏览 1 评论 0原文

我有以下两个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦旅人picnic 2024-11-03 04:28:16

传递给 _.each 的匿名函数中的“this”绑定到匿名函数,而不是父对象。 正确绑定该对象

_.each 确实允许传递上下文对象,以便通过http://documentcloud.github .com/underscore/#each

因此,将引用传递给您尝试在每个的第三个参数中绑定到的对象:

class ShouldWorkNow
  constructor: () ->
    _.each(fields, ((f) -> @[f] = ko.observable('')),this)

'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:

class ShouldWorkNow
  constructor: () ->
    _.each(fields, ((f) -> @[f] = ko.observable('')),this)
孤君无依 2024-11-03 04:28:16

克雷格的答案是正确的,但另一种解决方案是将匿名函数定义为 绑定函数。在这种情况下,您可以编写

_.each(fields, ((f) => @[f] = ko.observable('')))

=> 将函数绑定到定义它的上下文,以便 this 在函数中始终表示相同的内容 no不管它怎么称呼。对于回调来说,这是一种非常有用的技术,尽管在 _.each 的情况下,它比传入 this 的效率要低一些。

您可以使用 Underscore 做同样的事情:但是

callback = _.bind ((f) -> @[f] = ko.observable('')), this
_.each(fields, callback)

=> 可以节省您大量的打字时间!

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

_.each(fields, ((f) => @[f] = ko.observable('')))

The => binds the function to the context in which it's defined, so that this 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 passing this in.

You could do the same thing using Underscore by writing

callback = _.bind ((f) -> @[f] = ko.observable('')), this
_.each(fields, callback)

but => saves you a lot of typing!

听风吹 2024-11-03 04:28:15

另一种解决方案(可以说是最具可读性和最高效的)是跳过 _.each 并使用 CoffeeScript 的 for...in 迭代:

for f in fields
  @[f] = ko.observable ''

您甚至可以对循环进行后缀处理以使这是一句简单的话:

@[f] = ko.observable('') for f in fields

请记住,CoffeeScript 中的循环不会创建上下文或影响范围;只有函数可以。

Yet another solution (arguably the most readable and efficient) is to skip _.each and instead use CoffeeScript's for...in iteration:

for f in fields
  @[f] = ko.observable ''

You could even postfix the loop to make it a one-liner:

@[f] = ko.observable('') for f in fields

Remember that loops in CoffeeScript don't create context or affect scope; only functions do.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文