Home > Article > Web Front-end > Analysis of the advantages, disadvantages and differences of the two ways to create closures in JavaScript_javascript skills
There are two commonly used ways to create closures in JavaScript.
Constructor method:
new function() { var 变量... }
Inline execution method:
(function() { var 变量... })();
What are the differences between them under the internal operating mechanism of JavaScript? Which way is better to create? What advantages does it have over closures created in other ways?
This is how I understand it:
Difference:
First: Submethods can share variables
Second: Internal sub-method shared variables
Compare:
I think inline is better;
Advantages:
Generally, inline creation requires memory on demand, because only locally executed variables are in memory. Related and dependent codes can be organized to minimize the risk of unexpected interactions and construct all Method execution variables must be stored in memory, which will affect the performance of the web page. It is recommended to delete the variables before exiting
Of course, this is just my personal understanding. In fact, generally using closures will cause memory leaks, especially under the IE kernel browser. It is best to kill the variables after they are used.
The above is the entire content of this article, I hope you all like it