PDA

View Full Version : can i make local function inside of function ?


umen
11-11-2002, 07:39 PM
Hello
i wander say if i have main function that using or more likely to say reusing some groups of lines of code
over and over again because of many local variables that the main function using .
now my question is can i make some kind of "sub" function (its not have to be "real" function " )
that will be inside the main function and will be abele to use the local variables BUT and that i could
call this "sub" function from inside the main function .
something like this : (pasado)

function MAIN()
{
var foo1
var foo2
function SUB(){
..do stuff with local var's foo1 or foo2
}

if (...) {
call SUB ()
}else {
call SUB ()
}
}


ok i hope you got my point here..
thanks!

dewcansam
11-11-2002, 07:57 PM
no

x_goose_x
11-11-2002, 07:59 PM
I dont see the point to this, why not just call external functions and read what they return?

umen
11-11-2002, 08:03 PM
the point is the use of local variables

x_goose_x
11-11-2002, 08:08 PM
why not just use global?

jkd
11-11-2002, 09:44 PM
Originally posted by dewcansam
no

But you can:

function test() {
var a = 1;
var b = 2;
function subfunction() {
alert([a, b]);
}
subfunction();
}

If you've ever worked with Java, then you may see the need for anonymous inner classes. It is similar to the need for anonymous inner functions in Javascript.

dewcansam
11-11-2002, 09:50 PM
I stand corrected. You are right. :o

edit:
Can I chalk that one up to learing something new everyday ?