There are two common ways to create closures in JavaScript.
Constructor method:
new function() { var variable... }
Inline execution method:
(function() { var variable... })();
What is the difference between them under the internal JavaScript operation mechanism? Which method is better to create? What are its advantages over closures created by other methods?
This is how I understand:
the difference:
First: Sub-methods can share variables
The second: Internal sub-methods share variables
Compare:
I think inline is better;
Advantages:
Generally, inline creation is to request memory as needed, because only locally executed variables are in memory, and relevant and dependent code can be organized to minimize the risk of unexpected interactions. All method execution variables must be saved in memory, which will affect the performance of the web page. It is recommended to delete the variable before exiting.
Of course, this is just my personal understanding. In fact, closures are generally used to generate memory leaks in the IE kernel browser. It is best to kill the variables after use.
The above is all about this article. I hope you like it