为什么Backbone.js模型的'on()'将'this'作为最后一个参数,如果它几乎总是这样? [英] Why does Backbone.js model's 'on()' take 'this' as last parameter if it's almost always going to be this?

查看:102
本文介绍了为什么Backbone.js模型的'on()'将'this'作为最后一个参数,如果它几乎总是这样?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚进入Backbone,有一点我不明白为什么模型的'on()'方法总是需要三个参数 - 事件,处理程序和上下文。

I'm just getting into Backbone, and one thing that I don't understand is why the 'on()' method for models always takes three arguments--event, handler, and context.

似乎几乎总是'this'用于上下文,我还没有看到任何其他用法。即使有,因为我还没有看到它,但它一定非常罕见。

It seems that almost always 'this' is used for context and I haven't seen any other usage. Even if there were, since I haven't seen one yet it must be pretty rare.

所以我的问题是:什么时候使用除了'this'之外的上下文,为什么Backbone以这种方式设计?顺便说一下,我确实理解为什么你需要提供上下文,这只是我想知道为什么方法语法指定我使用三个参数而不是使最后一个参数可选 - 这似乎总是'这'并且感觉多余。我确定我错过了什么。请有人帮我理解。谢谢!

So my question is: When does one use a context other than 'this', and why is Backbone designed this way? By the way, I do understand why you need to provide context, it's just that I wonder why the method syntax specifies that I use three arguments instead of making the last argument optional--which seems to be always 'this' and feels redundant. I'm sure I'm missing something. Please someone help me understand. Thank you!


  • 为什么不能这样做:

  • Why can't one do something like:

model.on = function(event, callback){
  model.on_with_three_args.call(this, event, callback, this);
});

model.on_with_three_args = function(event, callback){
  /* whatever the on() is supposed to do */
});


推荐答案

假设我们在一个基于模型的视图中,我们想要绑定到模型的更改事件:

Suppose we're in a view that's based on a model and we want to bind to the model's change event:

this.model.on('change', this.render);

调用看到两件事:

The on call sees two things:


  1. 事件名称,一个简单的字符串。

  2. 处理程序,一个函数。

on 无法知道这个表示 this.render ,它只是看到一个函数; 上的甚至不知道上述电话与此之间的区别:

on has no way of knowing what this means in this.render, it just sees a function; on won't even know the difference between the call above and this:

this.model.on('change', function() { ... });

如果您的函数需要特定的上下文,那么您有两个选择:

If your function needs a particular context then you have two choices:


  1. 使用 _。bind创建绑定函数 _.bindAll 功能。绑定 $。proxy ,CoffeeScripts => var _this = this 闭包技巧,或任何创建或模拟绑定函数的方法。

  2. 告诉它你想要的上下文:

  1. Create a bound function using _.bind, _.bindAll, Function.bind, $.proxy, CoffeeScripts =>, the var _this = this closure trick, or any of the ways of creating or simulating a bound function.
  2. Tell it which context you want by saying:

this.model.on('change', this.render, this);


没有办法将调用堆栈展开到看看你想要哪个这个所以你必须明确它。

There's no way to unroll the call stack to see which this you want so you have to be explicit about it.

Backbone会像这样调用回调:

Backbone will call the callback like this:

node.callback.apply(node.context || this, ...);

其中 node.callback 是回调函数和 node.context 上给予的第三个参数(如果有的话)。如果你没有指定上下文那么你将得到任何这个恰好在调用 trigger 时;在上面的示例中,最终将成为模型。

where node.callback is the callback function and node.context is the third argument (if any) given to on. If you don't specify the context then you'll get whatever this happens to be when trigger is called; in the example above, this would end up being the model.

所以第三个参数为 on 实际上是可选的,但是默认值并不是非常有用,并且无法选择更好的默认值,因此无法在JavaScript中访问选择合理上下文所需的信息。这就是你在Backbone视图中看到这么多 _。bindAll(this,...)样板的原因。

So the third argument to on actually is optional but the default value isn't terribly useful and there is no way to choose a better default, the information you need to choose a sensible context simply isn't accessible in JavaScript. This is why you see so much _.bindAll(this, ...) boilerplate in Backbone views.

如果你尝试过这样的事情:

If you tried something like this:

model.on = function(event, callback){
    model.on_with_three_args.call(this, event, callback, this);
});

然后在这种情况下通常会是 model 所以你真的要说:

then this in that context would usually be model so you'd really be saying:

model.on = function(event, callback){
    model.on_with_three_args.call(model, event, callback, model);
});

model.on = function(event, callback){
    model.on_with_three_args(event, callback, model);
});

并且没有任何意义。 > 中的 的值与 c>的值在上调用的代码中。 JavaScript中的这个不是变量,它是一个引用当前调用上下文的关键字。

and there's little point to any of that. The value of this inside on has little if anything to do with the value of this in the code that calls on. this in JavaScript is not a variable, it is a keyword which refers to the current calling context.

这篇关于为什么Backbone.js模型的'on()'将'this'作为最后一个参数,如果它几乎总是这样?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆