Home > Article > Web Front-end > Introduction to the general method of eliminating closures in JavaScript_javascript tips
JavaScript’s closure is a feature that it actively develops, and it is also a feature that develops passively. In other words, on the one hand, JS can better solve some problems with closures. On the other hand, in order to solve certain problems, JS problem, and had to use closures to barely solve the problem.
The former will not be discussed here. If JS closures can solve the problem better, of course it is better to use closures.
What I am discussing is the latter, because of the limitations of JS itself, which have to be solved with closures, such as the requirement of "variables are only initialized once".
The conventional language is solved like this:
JavaScript usually solves this problem (using closures):
But I prefer this method (eliminating closures):
Because the latter has better scalability. When you need to implement different operations on a variable, the latter can just define a different function (that is, simple linear expansion), while the former ( closure) will need to be completely rewritten (which is why you often hear the word refactoring).