One can define a string of JavaScript to be compiled as a function with syntax
|
Target = new Function( [arg1,...argn], functionBody)
|
where the optional arg1,...argn are arguments for formal function defined in functionBody
|
Target is either the name of a variable or a property of an existing object or an event handler such as window.onerror
|
var setBGcolor = new Function("document.bgColor='antiquewhite'");
|
so you can now execute setBGcolor() where the function can be changed at any time with another setBGcolor = new Function("document.bgColor='pink'");
|
One can define event handlers which MUST be lower case and MUST have no arguments
-
window.onfocus = setBGcolor;
|
Note like eval, functionBody can be JavaScript constructed dynamically as in
-
var dynamic = 'bgcolor';
-
Function("document." + dynamic + "='antiquewhite'");
|