Answer the question
In order to leave comments, you need to log in
How to perform some action during any javascript function call?
So it became interesting, is it possible to call, say, console.log (1) with each call to any function?
I thought it would look something like this
Function.prototype.constructor = function (){
Function.prototype.constructor();
console.log(1);
};
function f() {
console.log(2);
}
f()
Answer the question
In order to leave comments, you need to log in
No. Function.prototype.constructor is just a Function reference to identify the constructor instance. Here, the redefinition of the Function constructor will not help - functions are usually set by a literal, without using it. You can do this with those created through the constructor, but do you need it? :)
window.Function = (function(Function){
return function(){
var fn = Function.apply(window, arguments);
return function(){
console.log('run');
return fn.apply(this, arguments);
}
}
})(Function);
Function('console.log(1)')() // => run 1
It would be safer to replace all methods of the prototype/object of interest with wrappers.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question