javascript - understanding pure js Event function -


i going through source of pace.js , came across following function , ::

evented.prototype.trigger = function() {   var args, ctx, event, handler, i, once, _ref, _ref1, _results;   event = arguments[0], args = 2 <= arguments.length ? __slice.call(arguments, 1) : [];   if ((_ref = this.bindings) != null ? _ref[event] : void 0) {     = 0;     _results = [];     while (i < this.bindings[event].length) {       _ref1 = this.bindings[event][i], handler = _ref1.handler, ctx = _ref1.ctx, once = _ref1.once;       handler.apply(ctx != null ? ctx : this, args);       if (once) {         _results.push(this.bindings[event].splice(i, 1));       } else {         _results.push(i++);       }     }     return _results;   } }; 

now understanding plain js, bit of challenge me, still went through independent components , methods used in function, not able figure out function doing in plugin.

can explain? if can give me genuine general idea enough.

also can function used independent of plugin pace.js? plugin pace.js seems written in pure js.

edit: kind if can comment out whats happening on each line, if use technical terms that's fine. can later refer mdn , understand once know whats vaguely happening on each line. understand js method , web api, mdn has great documentation on it, everywhere see event that's confused.

the code you're looking @ compiled coffeescript code, looking @ original code makes easier understand:

  trigger: (event, args...) ->     if @bindings?[event]       = 0       while < @bindings[event].length         {handler, ctx, once} = @bindings[event][i]          handler.apply(ctx ? @, args)          if once           @bindings[event].splice i, 1         else           i++ 

src: https://github.com/hubspot/pace/blob/20a7a70028f51c5e511bcb98be5e116f577faa9f/pace.coffee#l164

it checks if there binding given event, goes through existing bindings , calls handlers event.


Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -