In Javascript, how do I make a function calls a specific function before executing anything within it? -
this question has answer here:
- add line of code functions 3 answers
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
Post a Comment