[Javascript] Re: Replace/modify javascript

Alberto .·. Vallini vallinis at yahoo.com
Mon Oct 27 13:42:08 CST 2003


Hallo,
I hope I got your question right.
So, you can try to "study", so to say, the following
code a bit: I do not imply it is "the" solution or an
optimal solution, yet I imply it works (I nested many
alerts to show to you what's going on):

<script language="JavaScript"><!--
function Plus(iArg1, iArg2) {
var foo;
//label
alert(iArg1 + iArg2);
//endlabel
}
alert(Plus)

function swap(){
//label
alert("swapped");
//endlabel
}
var change=Plus.toString();
change=change.substring(change.indexOf("//label"),
change.indexOf("//endlabel")+"//endlabel".length);
alert("change=\n\n\n"+change);
var changed=swap.toString();
changed=changed.substring(changed.indexOf("//label"),
changed.indexOf("//endlabel")+"//endlabel".length);
alert("changed=\n\n\n"+changed);
var prepare=Plus.toString().replace(change, changed);
alert("prepare=\n\n\n"+prepare);
prepare=prepare.substring(prepare.indexOf("{")+1,prepare.lastIndexOf("}"));
alert("now prepare=\n\n\n"+prepare);
Plus=new Function("iArg1, iArg2",prepare);
alert(Plus.toString());
Plus();
//--></script>
 
the constructor new Function (capital F) takes as
arguments 2 strings, the first the arguments, the
second the body of the function exclusive of the
starting and trailing curly brackets.
ciao,

=====
------------------------------------
Alberto Vallini
http://www.unitedscripters.com/
"No one applauds a tenor because he is clearing his throat." [Les Liaisons Dangereuses]
------------------------------------

__________________________________
Do you Yahoo!?
Exclusive Video Premiere - Britney Spears
http://launch.yahoo.com/promos/britneyspears/



More information about the Javascript mailing list