How to reference parent parameters of an anonymous function Javascript -
i'm trying figure out how can reference argument on wrapper function stored in variable calls anonymous function. such in example below. problem run i'm used accessing parameters via arguments variable, sees parameter myfunc. know it's supposed possible, can't figure out how.
var myfunc = function(arg1, arg2){//do stuff} var myfuncwrapped = wrapper(myfunc); myfuncwrapped('arg value1', 'arg value2'); function wrapper(func){ //how can reference 'arg value1' here since arguments == myfunc? }
as comment suggested, wrapper should returning function can capture arguments via closure when myfuncwrapped gets called.
var myfunc = function(arg1, arg2) { console.log(arg1); // (for testing only) should be"arg value1" }; var myfuncwrapped = wrapper(myfunc); myfuncwrapped('arg value1', 'arg value2'); function wrapper(func) { /* anonymous function below 1 * being called when invoke "myfuncwrapped" has arguments need. */ return function() { console.log(arguments[0]); // (for testing only) should be"arg value1" func.apply(this, arguments); } }
Comments
Post a Comment