Home >Web Front-end >JS Tutorial >Analysis of closure principles in JavaScript_javascript skills
Let's look at a definition:
Closure
The so-called "closure" refers to an expression (usually a function) that has many variables and an environment bound to these variables, so these variables are also the expression part of the formula.
This shows that closure in JavaScript is a function that contains context. In other words, the basis of this function is the environment in which it is located. This cannot be surpassed. Is it a bit familiar to linear algebra? What about the feeling?
Looking at it from another perspective, the function of closure is to achieve OO. In JavaScript, there are no public, private, and protect attribute marks like C, so it is difficult to establish a class. "A class is data with behavior, and a closure is behavior with data." In JavaScript, we replace the definition of a class with the definition of a function, and replace the setter/getter method with a closure. Please look at a piece of livecode: