How to dynamically change the contents of a function using JavaScript -
to understand function in html page , generated, cannot change generated code:
function update_qu7260() { var newval = '' for( var idx = 0; idx < 2; idx++ ) { var test if( idx == 0 ) test = text7263 else if( idx == 1 ) test = text7265 if( test.matchobj ) newval += test.leftsel + "-" + test.matchobj.rightsel + "," } newval = newval.substring( 0, newval.length-1 ) varquestion_0001.set( newval ) qu7260.hasbeenprocessed=false; doimmfeedback('qu7260'); } var qu7260 = new object(); ... qu7260.updfunc = update_qu7260; var qobj=[qu7260];
note in above number "7260", numbers start @ 1 there lots of them , each update_###()
different cannot re-write them "hard wired" code. code in external javascript file , executed onload:
... var updfunc = qobj[0].updfunc.tostring(); if(updfunc.indexof('doimmfeedback(')!=-1){ updfunc = updfunc.replace('doimmfeedback','doimmquestionfeedback'); // function updfunc = updfunc.replace('function ',''); // remove word function var funcname = updfunc.substr(0,updfunc.indexof('(')); // function name e.g. update_qu7260 updfunc = "window['" + funcname + "']=function" + updfunc.replace(funcname,''); eval(updfunc); } ...
when change eval() alert() can see it's correct, however, eval() not raising errors , function doimmquestionfeedback
not being called. when subsequently alert(qobj[0].updfunc.tostring())
see original function.
it seem have provided information complex, following code better example:
function hi(){alert('hi');} function changehi(){ hi(); // alert box hi newhi = "function hi(){alert('hi there');}" eval(newhi); hi(); // alert box hi window.settimeout('hi()',500); // alert box hi } window.settimeout('changehi()',500);
the following original question:
i have predefined function did not create, however, know it's name can function , change doing:
var functext = window.updatefunc.tostring(); functext = functext.replace('dosomeotherfunction(','domyfunction(');
how update actual function did before except call domyfuntion()?
the following example visualize want do, actual function need change complex. have:
function updatefunc(whattoupdate,true){ ... - lots of stuff. var retval = dosomeotherfunction(whattoudate); ... - lots of stuff based on retval }
i need change to:
function updatefunc(whattoupdate,true){ ... - lots of stuff var retval = domyfunction(whattoudate); ... - lots of stuff based on retval, have had chance change retval }
then first thing function call dosomeotherfunction() check/change returned value , subsequently return value updatefunc().
i have tried manipulate functext above to:
functext = 'window.updatefunc = function(...'; eval(functext);
without success.
this may closed enough looking for.
assuming have original function:
function originalfunc(val) { // function converts input string upper case return val.touppercase(); }
now want override either before or after execute function (in example, execute before, of course before or after doesn't matter in case).
// preserve orignal function var originalfunc_save = originalfunc; // override original function block var originalfunc = function(text) { // lets call orignal function text = originalfunc_save(text); // our custom thing return text.split('').reverse().join(''); }
so our test should work.
var text = 'this test'; console.log(originalfunc(text));
output:
tset si siht
this method works if have override functions inside class. thing have careful of choose saved name doesn't interfere original class code. _save may not enough, idea.
update: i'm updating code above use string variable pointing original function. think op wanted.
original code defined library
function originalfunc(val) { // function converts input string upper case return val.touppercase(); }
now use func string variable point function , execute it.
var text = 'this test'; var func = 'originalfunc'; text = window[func](text); console.log(text);
output: of course original intended result because haven't overridden it.
this test
now write our code override original function behavior using string pointing function.
// let's define new function string var funcsaved = func + '___saved'; // preserve original function code window[funcsaved] = window[func]; // override original function code block window[func] = function(text) { // lets call orignal function text = window[funcsaved](text); // our custom thing return text.split('').reverse().join(''); } // let's test code text = 'this test'; text = window[func](text); console.log(text);
output:
tset si siht
Comments
Post a Comment