Home > Web Front-end > JS Tutorial > JavaScript Intermediate Notes Chapter 4 Closure_javascript skills

JavaScript Intermediate Notes Chapter 4 Closure_javascript skills

WBOY
Release: 2016-05-16 18:46:32
Original
734 people have browsed it
5. Closure
Closure means that the inner function can reference variables that exist within the function surrounding it, even if the execution of the outer function has terminated.
Let’s first look at an example of closure.

[Ctrl A Select all Note: If you need to introduce external Js, you need to refresh to execute
]

Code ① is inside a function layer, but it can use the variable num of the outer layer.
Closure can also solve another common JS problem, the impact of global variables.
By automatically executing anonymous function combination closures, variables that are originally global can be hidden. Look at the example below:

[Ctrl A Select all Note:
If you need to introduce external Js, you need to refresh to execute
]
When using setTimeout, we Closures are often used as well.
[Ctrl A Select all Note:
If you need to introduce external Js, you need to refresh to execute
]
Use setTimeout in this way () can avoid some problems.

[Ctrl A Select all Note:
If you need to introduce external Js, you need to refresh to execute
]
We can use the following code to solve it:


[Ctrl A Select all Note:
If you need to introduce external Js, you need to refresh to execute
]By using closures, Domain control, thus meeting our requirements.
The above code can be broken down into:
[Ctrl A select all Note: If you need to introduce external Js, you need to refresh to execute ]
The concept of closure is not easy to grasp, and it took me a lot of time and energy to understand it.
6. Summary
Notes (2), (3), and (4) explain several important contents in JavaScript, including references, function overloading, scope, context objects, and closures.
Key contents of reference: pointer, array reference, string reference, difference, pass by value, pass by address.
Key contents of function overloading: number of parameters, type of parameters, arguments, pseudo array, typeof, constructor, difference - string and object.
Key contents of scope: function division, global scope, global object, properties of window object, local scope, explicit declaration, implicit declaration.
Key contents of context object: this variable, call, apply, parameter difference, array.
Key contents of closure: inner function, outer function, variable, setTimeout, closure problem, last assignment, closure and scope.
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template