Чисто из интереса, решил узнать, можно ли повесить событие на вызов функции. Как ни странно (для меня), это удалось.
(function(){
window.callEvent = function(func, callback){
var callNumber = 0;
return function(){
var args = [].slice.call(arguments);
var result;
try {
result = func.apply(this, arguments);
callNumber++;
} catch (e) {
callback(e, args, this, callNumber);
throw e;
}
callback(result, args, this, callNumber);
return result;
}
}
var logSomething = function(something){
console.log(something);
}
logSomething = callEvent(logSomething, function(result, args, self, callNumber){
console.log('alertSomething has been called. \n\
Result is ' + result + '. \n\
Arguments are ' + args + '. \n\
Self is ' + self + '. \n\
Number of calls ' + callNumber + '. \n'
);
});
Array.prototype.push = callEvent(Array.prototype.push, function(result, args, self, callNumber){
console.log('Array.prototype.push has been called. \n\
Result is ' + result + '. \n\
Arguments are ' + args + '. \n\
Self is ' + self);
});
logSomething('Fuck yeah!');
logSomething('Fuck yeah!');
logSomething('Fuck yeah!');
logSomething('Fuck yeah!');
logSomething('Fuck yeah!');
var array = [1,2,3,4,5];
array.push(6);
})();