The way you have the code, you cannot. You do not allow access to privateStuff . It seems to me that you need to familiarize yourself with the concept of closure (which is fundamental to JavaScript).
Whenever you use the f() { ... } construct in JS (and several others, such as try-catch ), you always implicitly create a closure. Nesting functions, just like you, are completely legal, but if you do not give an external function an external reference to an internal function, the internal function can only be accessed from an external function.
From Mozilla :
You can insert a function inside a function. A nested (internal) function is private for its containing (external) function. It also forms a closure.
Closing is an expression (usually a function) that can have free variables along with the environment that binds these variables (which "closes" the expression).
Since a nested function is a closure, this means that a nested function can "inherit" the arguments and variables of its contents. In other words, the inner function contains the scope of the outer function.
- An internal function can only be obtained from external function operators.
- The inner function forms a closure: the inner function can use arguments and
Although the Mozilla documentation immortalizes this, itβs a little incorrect to say that JavaScript is private or public , because the nomenclature has a very clearly defined meaning in most programming languages ββ(such as C ++ and Java) that are polymorphic behavior or inheritance. As for JS, itβs better to think of it as a volume limit and try to get a clear idea of ββclosing.
source share