+ 1
Does nesting a function inside a function makes the inner function private?
3 ответов
+ 3
Yes and no. Typically you cannot access an inner function from outside the parent function. However, there are way that you can build your functions to give you some level of access to the inner function.
For instance you can't access the function inner below:
function outer() {
function inner() {
alert("hello");
}
}
inner() // undefined
outer().inner() // undefined
outer.inner() // undefined
But if you build your function so that it exposes the inner function:
function outer() {
function inner() {
alert("hello");
}
outer.inner = inner;
}
outer(); // outer function needs to be called to create the outer.inner variable.
outer.inner(); // now you have access to the inner function and the alert outputs hello.
+ 1
Short answer: Yes.
Long answer: https://softwareengineering.stackexchange.com/a/253528