Click to See Complete Forum and Search --> : Embedded Javascript


kheidt
01-21-2003, 09:34 AM
I copied some javascript from a download site. I am a little confused because this code contains multiple functions. The first function calls the other two functions. However the called functions are defined within the opening and closing brackets of the calling function.

Is it okay to embed a function definition within the definition of another function? I wouldn't think so but the JS works!

i.e.
function x()
{
var xx = y();
var xxx = z();

function y()
{
.....
}

function z()
{
.....
}

}

Dan Drillich
01-21-2003, 10:27 AM
‘JavaScript the Definitive Guide’ says –
Functions definitions usually appear in top-level JavaScript code. They may also be nested within other function definitions, but only at the “top level” of those functions; that is, function definitions may not appear within if statements, while loops, or any other statements.

Cheers,
Dan