I've frequently had need to have my own library emit events via callback subscriptions and in some cases I prefer to just use Javascript function variables directly rather than some third party library or the DOM to manage events. I've found two strategies and I'm not sure which one is more appropriate.
These strategies are demonstrated with this JSFiddle: http://jsfiddle.net/Q8pQT/2/
The first strategy is to add each subscription to a function array, so when I am ready to invoke an event I can iterate through all callbacks in the event.
var fnactionidx = 0;
var fnarr = [];
function add2fnArray() {
fnarr.push(fnaction);
console.log("added to array", fnarr.length);
}
function fnaction() {
console.log("Hi.", ++fnactionidx);
}
function arraygo() {
// do internal stuff here first
fnactionidx = 0;
for (var i=0; i<fnarr.length; i++) {
fnarr[i]();
}
console.log("array complete!")
// do internal stuff here last
}
Subscribing in this example is done with a simple Array.push(fn).
The second strategy is to define an initial function, then for each subscriber, wrap it with a containing function.
function fnactionnested() {
console.log("nested complete!");
// do internal stuff here last
}
function nestfn(fn) {
var na = fnactionnested;
fnactionnested = function() {
fn();
na();
}
console.log("nested " + fn.toString());
}
function nestgo() {
// do internal stuff here first
fnactionidx = 0;
fnactionnested();
}
Subscribing in this example is done with nestfn. Note that I don't really care which sequential order the execution occurs in since it's explicit; in the first example I could've used Array.unshift.
I've seen the 2nd one recommended more. My suspicion is that, while the 2nd one is more Javascript-like in that it takes advantage of Javascript being flexible with functions as a functional language, it probably introduces significantly more memory overhead since you have functions inside of wrapper functions inside of wrapper functions, and so on, depending on how many subscribers there are.
Any thoughts on which approach is preferable?
Note that I do not want to use a string-based event subsystem, I only want to distinguish between these two strategies, or if there is another callback strategy that does something very similar to these two without going through some "global" event system with some string key.