Possible Duplicate:
JavaScript: var functionName = function() {} vs function functionName() {}
In Javascript, "private methods" are basically just inner functions. But throughout the various javascript tutorials available online, there seems to be two practices for declaring private methods:
function foo()
{
var privateMethod = function() { }
}
versus...
function foo()
{
function privateMethod() { }
}
They both seem to achieve the same effect, i.e. privateMethod
is not accessible publicly through an instance of foo
. The only difference seems to be with the first way (using the var
keyword), privateMethod
is only available to code that comes after the declaration. But with the second way, privateMethod
is available to all code within foo
. So, is there any other difference that makes either of these two practices preferable?