The advantages and disadvantages of the two methods for creating closures in JavaScript and the advantages and disadvantages of javascript
There are usually two common methods for creating closures in JavaScript.
Constructor method:
New function () {var variable ...}
Inline execution method:
(Function () {var variable ...})();
What are their differences in the JavaScript internal operating mechanism? Which method is better to create? What is its advantage over the closure created by other methods?
I understand it as follows:
Differences:
First: Sub-methods can share variables.
Second: internal sub-methods share Variables
Comparison:
I think inline is better;
Advantages:
In general, inline creation requires memory on demand, because only the locally executed variables are in the memory, and relevant and dependent code can be organized, in order to minimize the risk of unexpected interactions, the execution variables of all the methods to be constructed must be saved in the memory, which will affect the performance of the Web page. We recommend that you delete the variables before exiting.
Of course, this is just my personal understanding. In fact, it is generally necessary to use closures to generate memory leaks especially in IE kernel browsers. It is best to finish using the variables.
The above is all the content of this article. I hope you will like it.