Note that there are some explanatory texts on larger screens.

plurals
  1. PORemove and add elements to functions argument list in javascript
    text
    copied!<p>I have written an observer in javascript. You can trigger Events like this:</p> <pre><code>ClassThatExtendsEvent.triggerEvent('EventName', arg1, arg2, ..., argn) </code></pre> <p>There is a variable number of arguments, with the first argument being the event name. I do as well have subscribers, that are saved in a private property of my event class. They are getting called like this:</p> <pre><code>Event.prototype.triggerEvent = function() { var callbacks = this._events[eventName]; if(callbacks !== undefined) { for (var i = 0, l = callbacks.length; i &lt; l; i++) { // I do not need the first argument here!!! callbacks[i].apply(callbacks[i], arguments); } } } </code></pre> <p>However, the subscribers do know the event name, hence it could be ommitted. But I do not know, how to remove one argument from the list (as it's not an array but seems to be some kind of object).</p> <p>I also need to readd the event name, once it's been deleted, because after notfiying the direct subscribers, a global event system kicks in. This global event system needs the name.</p> <p>Hence the entire function looks like this:</p> <pre><code>Event.prototype.triggerEvent = function() { // First we deliver to direct subscribers var callbacks = this._events[eventName]; if(callbacks !== undefined) { for (var i = 0, l = callbacks.length; i &lt; l; i++) { // I do NOT need the first argument here!!! callbacks[i].apply(callbacks[i], arguments); } } // Now we broadcast the event to the entire system // I DO need the first argument here!!! Facade.getInstance().broadcastEvent.apply(Facade.getInstance(), arguments); } </code></pre> <p>How would you implement this?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload