如果它几乎总是这样

如果它几乎总是这样

本文介绍了为什么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:

 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'作为最后一个参数,如果它几乎总是这样?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-20 18:17