March 18, 2001 - Modifying Globals
March 18, 2001 Modifying Globals Tips: March 2001
Yehuda Shiran, Ph.D.
|
var global = 5;
function first(param) {
global += 10;
alert(param + " method shows global to be " + global);
}
Pass "first"
to this function and see that you get the expected alert box with the global variable incrementing by 10. The second way to define a function is the anonymous way. You define a function inline, without giving it a name:
second = function(param) {
global += 10;
alert(param + " method shows global to be " + global)
};
Pass "second"
to this function and see that you get the expected alert box, with the global variable incrementing by 10. The third way to define a function is by constructing it with the Function
function. The Function
function accepts two parameters: the parameter to be passed, and the body of the function:
third = new Function(
"param",
" global += 10; alert(param + ' method shows global to be ' + global)");
Pass "third"
to this function and see that you get the expected alert box, showing global
incrementing by 10 with each click.