Javascript Find argument passed to a function -



Javascript Find argument passed to a function -

i needing find argument passed function function.

let suppose have function called foo:

function foo() { var = 3; var b = "hello"; var c = [0,4]; bar(a - b / c); bar(c * + b); } function bar(arg) { alert(arg) }

as now, of course, bar alert nan.

inside of function bar, want obtain argument in form passed. furthermore, want able access values of a, b, , c bar function. in other words, of nature:

bar(a - b / c); function bar() { //some magic code here alert(originalarg); //will alert "a - b / c" alert(vars.a + " " + vars.b + " " + vars.c); //will alert "3 hello 0,4" }

you may not think possible, know can weird things javascript. example, can display code of calling function this:

function bar() { alert(bar.caller); }

i willing bet few dollars sort of finagling can original form of argument , values of variables in argument.

i can see how if allowed phone call bar in form this:

bar(function(){return - b / c}, {a: a, b: b, c: c});

but convoluted , hence unacceptable. way bar called may not changed.

your question misguided , sick and...i love it! here short solution explain.

function foo() { var = 3, b = "hello", c = [0, 4]; bar(a - b / c); } function bar(n) { alert([n, a, b, c, eval(n)].join(' ')); } function meld(f, g) { f = f.tostring(); g = g.tostring(); f = f.replace(/function\s+\w+/, 'function '); var n = g.match(/function\s+(\w+)/)[1]; f = f.replace(new regexp(n + '\\((.*?)\\)', 'g'), n + '("$1")'); var = f.indexof('{') + 1; f = f.slice(0, i) + g + f.slice(i); eval('var ret = ' + f); homecoming ret; } foo = meld(foo, bar); foo();

the key meld(f, g) function returns new anonymous function acts f while calling , exposing internals re-create of g. see, original g in bad position see f -- @ best can utilize g.caller , tons of regular expressions.

here pseudo code meld. first create f anonymous function, suitable evaluating , assigning variable later on; instance function foo() { becomes function() {. next find g's real name , quote arguments passed new f. next, insert re-create of g within new f. mask global name , have access f's local variables if own. finally, evaluate anonymous function , homecoming it.

so how utilize meld? replace foo meld(foo, bar) , utilize foo would.

ok, limitations. didn't want spend lots of effort refining regex within meld quote g's argument(s) against possibilities. have been distraction concept of solution whole. you'll need alter handle more 1 argument , escape have quotes or parentheses.

javascript argument-passing

Comments

Popular posts from this blog

iphone - Dismissing a UIAlertView -

c# - Can ProtoBuf-Net deserialize to a flat class? -

javascript - Change element in each JQuery tab to dynamically generated colors -