I want to create my own signal emitter or callback storage, call it whatever you want.
Here is what I come up so far:
var DetonationCallback = function detonationCallback() {
detonationCallback.callbacks = [];
detonationCallback.add = function(callback) {
detonationCallback.callbacks.push(callback);
};
for(var i = 0; i < detonationCallback.callbacks.length; ++i) {
callback[i](arguments);
}
};
Basically I have two problems now which I can not tackle. The first one is how can I move
detonationCallback.callbacks = [];
detonationCallback.add = function(callback) {
detonationCallback.callbacks.push(callback);
};
outside of the function? And another question is how can I pass all the arguments which were passed into the detonationCallback just in the same order into callback[i]?
Will be grateful for any answer.
Additional info: when done with the implementation of the type I would like to use it as follows:
var callback = new DetonationCallback();
function f1() {
}
function f2(firstArg, secondArg) {
}
callback.add(f1);
callback.add(f2);
callback();
newkeyword. Thenewkeyword returns a new instance ofDetonationCalback. You can'treturnanything if you're calling a constructor.