Home > Web Front-end > JS Tutorial > Analysis of the advantages, disadvantages and differences of the two ways to create closures in JavaScript_javascript skills

Analysis of the advantages, disadvantages and differences of the two ways to create closures in JavaScript_javascript skills

WBOY
Release: 2016-05-16 15:53:41
Original
1096 people have browsed it

There are two commonly used ways to create closures in JavaScript.

Constructor method:

new function() { 
var 变量... 
} 

Copy after login

Inline execution method:

(function() { 
var 变量... 
})(); 

Copy after login

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

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template