I understand what a nested function is, but I don't understand why we even need nested functions in the first place. Is there a problem that can only be solved by using nested functions in JavaScript. All the examples I see which create nested function, can be coded without creating a function inside a function and will result the same. So which problem requires creation of nested functions, and can only/efficiently be solved by using nested functions.
相关问题
- Is there a limit to how many levels you can nest i
- How to toggle on Order in ReactJS
- void before promise syntax
- Keeping track of variable instances
- Can php detect if javascript is on or not?
It's not about efficiency but about the paradigm. If you use the script paradigm you can just code away without worries. If you use the classical paradigm, you need to define classes but you don't need nested functions. Still, in scripting and classical, you MAY use nested functions if you want to.
Only if you switch to the functional paradigm, which is the actual "native" paradigm of javascript, you NEED nested functions in some cases.
As long as you do not use functional programming you'll not leverage all the power of javascript. This is an example of a factory that uses a closure that cannot be done otherwise (there are several other uses for nested functions, not just a closure):
The user code will do:
Without computing anything. Say, it takes a lot of time to compute this and you don't want to compute it unless necessary. You can now pass the function
f
to other code that is not yours (e.G. jQuery event handler code):This
f
will be executed when you click on#id
, but you cannot do anything about it. The argumentsa
andb
are already embedded whencreate()
was called.This means that you can have information hiding and leverage contextual scoping. Imagine that the creator and the user are two different codebases not written by the same person. Or that
a
andb
are secrets to be kept as such from the other codebase.Look up the various words I sprinkled in the text above to learn more about functional programming.
First of all, there are several ways of doing a task in programming But among them we have to choose a method which is efficient and faster in terms of memory and execution time.When you need to switch from a main program to subroutine then time for context switch is more than just performing a inline task if size of subroutine is smaller..So overall its benificial if size smaller otherwise you always have a alternate path..Also a Nested function have the access to the scope "above" them.It will reduce the headache of passing a argument to function and reuse it somewhere else in the program..All above variables and functions are accessible to this Nested function and can update them..
JavaScript variables can belong to the local or global scope.
Global variables can be made local (private) with closures.
functions in JS are like variables, so if may want to use nested functions to be for local use.
The core importance of nested functions is scope generation. We need nested functions and scopes in JavaScript to achieve the following.
Here is a sample module that displays the power of encapsulation offered by function nesting and scopes:
The code above gives us the power to control which things to expose. In this specific case all members attached to the _externals object are exposed to the global namespace via reference to notificationService object. Without using scoping, internal members (_jqExtend and _showMessage) would also be attached to the window object and increase the effort required by the browser to resolve identifier references.