I found myself using a weird way to add callback functions to my functions and I was wondering if there is a more generic way to add callbacks to functions, best case I would ha
You could, if you really want to, extend Function.prototype with a .cb prototype. Something like:
Function.prototype.cb = function(cb){
var self = this;
return function(){
self.callback = cb || function(){};
self.apply(self, arguments);
}
}
then your code would compress to:
var myFunc = function(obj){
// some code
this.callback(); // callback will always be defined
}
and the call would slightly change:
myFunc.cb(function(){
// callback code
})(myObj);
Just an idea. You can make the syntax pretty much whatever you want.