In Javascript, how do I make a function calls a specific function before executing anything within it? -


this question has answer here:

in javascript, how make function calls specific function before executing within (similar super.func in oo language)?

e.g.

function test() {   // call custom function }  function test2() {  // call same custom function } 

the idea of can achieve things logging out function gets called, without having inject log statement beginning of every single function.

javascript not have such feature can done in automatic way. you'd have replace test , test2 else calls logging thing , calls original.

for example, here's generic function can hook other function , call log thing first:

function test() {   // function whatever }  // function hooker function logfunction(fn, logfn, arg) {     return function() {         logfn(arg);         return fn.apply(this, arguments);     } }  // custom function called hook function before executing original function function mylogger(msg) {     console.log(msg); }  // hook test = logfunction(test, mylogger, "test"); 

Comments

Popular posts from this blog

java - UnknownEntityTypeException: Unable to locate persister (Hibernate 5.0) -

python - ValueError: empty vocabulary; perhaps the documents only contain stop words -

ubuntu - collect2: fatal error: ld terminated with signal 9 [Killed] -