javascript in the private scope

We know that all the js block-level scope is invalid, variable block-level in scope, can still be read on the outside, in fact, stated at the outside. How privatization variables, only the scope of onset at the block level, to avoid polluting the global variables it. Further, it is difficult to mount in the global variable is recovered, but the function of which variable is finished will be recovered (no internal function reference to a function within function variables, also known as closures)

javascript introduced immediately execute the function to create private scope.

Description of the book

 

Then give some examples to illustrate the use of the actual code

Open node_module vue referenced vue.js

This code is shorthand structure

(function(global, factory){

}(this, function(){}))

This is a private scope

In addition vue.js, the conventional structure is also such jquery

The external structure is drawn to

(function(window, underfined){})(window);

This is also a private scope, execute the function immediately

If you write your own components and frameworks, remember to use a package module code to execute the function immediately. And immediately function completes execution, internal variables can be recycled.

Memory Problems

 

Guess you like

Origin www.cnblogs.com/chenyi4/p/12009694.html