骨干triggerEvents变量 [英] Backbone triggerEvents variable

查看:45
本文介绍了骨干triggerEvents变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读Backbone.js源代码,对此感到困惑

I'm reading through the Backbone.js source and am confused by this

var triggerEvents = function(events, args) {
  var ev, i = -1, l = events.length;
  switch (args.length) {
    case 0: while (++i < l) (ev = events[i]).callback.call(ev.ctx);
    return;
    case 1: while (++i < l) (ev = events[i]).callback.call(ev.ctx, args[0]);
    return;
    case 2: while (++i < l) (ev = events[i]).callback.call(ev.ctx, args[0], args[1]);
    return;
    case 3: while (++i < l) (ev = events[i]).callback.call(ev.ctx, args[0], args[1], args[2]);
    return;
    default: while (++i < l) (ev = events[i]).callback.apply(ev.ctx, args);
  }
};

我有很多问题-

  1. 为什么这是触发事件的优化方法(如带注释的消息来源所述)?
  2. 什么是ev.ctx?
  3. 什么是.callback()?

此结构意味着,如果有机会,为了速度的考虑,应该始终使用call而不是应用,因为此函数的结构似乎在说:如果我知道有多少个use调用,则使用申请",那么就可以一直使用申请".

This structure implies that if given the chance one should always use call as opposed to apply in the interests of speed, as the structure of this function seems to say "If I know how many args there are use call, instead, use apply", when one could just use apply all the way through.

简而言之,我不确定此功能的目的是什么,为什么不以编写方式来编写它,以及如果有人可以告诉我,那就太好了!

In short I'm not sure what the purpose of this function is and why it has been written the way it has been written and if someone could tell me it would be great!

推荐答案

我创建了一个小的

I created a small JSPerf test suite that compares Function.call and Function.apply performance. It shows quite clearly that (with Chrome 24) Function.call is faster by 30-50%. Try and run it in your browser to see how the performance differs.

但是,这并不意味着您应该在自己的代码中遵循此优化.Backbone事件功能是Backbone的核心,并且会触发很多事件.作者优化了这段代码,从中挤出了最后的性能.在大多数其他情况下,这可能是过度优化.

This doesn't mean, however, that you should follow this optimization in your own code. The Backbone events functionality is at the core of Backbone, and a lot of events are fired. The authors have optimized this piece of code to squeeze the last bits of performance out of it. In most other cases this would be an over-optimization.

ev.callback 属性是事件的回调函数.

The ev.callback property is the callback function for the event.

请考虑以下示例:

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

在这种情况下,回调是 this.handleChange 方法.

The callback in this case is the this.handleChange method.

符号(ev = events [i]).callback.call 只是

ev = events[i];
ev.callback.call

该快捷方式有效,因为在javascript中,赋值操作会返回分配的值.

The shortcut works, because in javascript an assignment operation returns the assigned value.

另一方面, ev.ctx 属性是作为 this 上下文绑定到回调函数的对象. Backbone.Events.on 将上下文作为可选参数.在上面的示例中,最后一个参数 this 指定回调函数的上下文应为包含类.

The ev.ctx property on the other hand is the object to bind as the this context to the callback function. Backbone.Events.on takes the context as an optional argument. In the above example the last argument, this, specifies that the callback function's context should be the containing class.

这篇关于骨干triggerEvents变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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