Why does not pollute the global variable IIFE

Seen in the development process IIFE, function expressions are anonymous, if we write it as a named function expressions, global scope that would not be one more variable, so how can we ensure that it does not pollute the global variable ?

Usually when we talk about IIFE, are talking about its use, modular, closures, etc., but few mention why it can be used to make modular, why does not pollute the global variable.

Although this article is saying IIFE, but not too much introduction of its use, or its practical significance (after all, has been discussed today in the standard ES6 this significance is not great), more of a discussion of such an approach in itself in the syntax on the establishment of the reasons why you can replace it, and the points involved appeared before the block-level scope.

Read judgment

Consider the following codes

(function a(){
    console.log('run a');
})();
console.log(a);
复制代码

This code is running in strict mode, said here about operating results, a can not print run here will complain.

There is confusion on the operating results? Perhaps this article will make you slightly and dissolve doubts.

IIFE establishment

IIFE can be seen as two parts, the first part of the definition of a function expression, which is the second half of the parentheses indicates the function to run. Take a concrete example to describe.

(function a(){
    console.log('run a');
})();
复制代码

In the above code in parentheses first half of the definition of a function expression, grammatically parentheses after the function represented by this function is to run. Of course, more than IIFE wording this kind, there are others, such as, with use of a function expression including parentheses and back up the other written in parentheses.

Since the standards set forth in the beginning of the function key is a function declaration, so it becomes a function of the expression, we need to add something, such as adding a +, void, at the beginning of the function - and other operators, in short, our aim is to let this line starts with function, so parsing formula would think function () {} () is an expression to run it as a function declaration rather than to resolve it. See the examples below:

console.log(+function s(){return 1}())      // 1
console.log(+function a(){})    // NaN
复制代码

As can be seen from the above over-run is performed when IIFE function () {} () as a whole come to a final operation result in the expression, of course, we do not care when using IIFE return results.

The reason does not pollute the global scope

IIFE reason does not pollute the global variable is the characteristic function expression.

Named function expressions

When we use the expression to create the function function, want to use the current function inside the body of the function, you can use a named function expressions. This function name only as a function of body internal variables. In other words, use a named function expressions created, and can not produce the same function and variable declarations in scope function declarations, but will only generate this variable inside the function name, and the variable is read-only, can not be assigned.

var a = function c() {
    c = 'test';
    console.log(c); // ƒ c() {
                    // c = 'test';
                    // console.log(c);
                    // } 
}
a();
console.log(c); // c is not defined
复制代码

Here we can understand, IIFE not contaminate the global variable is a characteristic function table by using the formula, derived therefrom various written, just to be able to identify when this expression is executed in the interpreter syntax, and then execute it. When you see the essence of this time, for it is written that we do not need to go to all sorts of mechanical memory, and we ourselves can also write a lot.

in conclusion

In essence, IIFE is to fully understand the language features combined with the needs of the times the product. Here is more characteristic function expression characteristics, of course, there are characteristics combine grammar part. Of course, demand for so-called front-end "slash and burn" era of component-based people to explore.

Placed under article inappropriate, but it is suddenly wanted to write the words: I remember an argument that the development of the front end of the other languages, but is already practiced years ago, the idea to move over it, so that the front no technical content, and even superiority. But I want to say is, thinking is not a language, or possession of a particular area. Not because young things other people are practicing practiced mind is that it is the copy of plagiarism, after all, practice with a spark ideas generated at different needs it can be completely different. Also to express at the front there are still many unique and interesting things, and this appears segments of demand, but is a product of the times, we are all for the needs of the service, so there is no need to produce so-called superiority or inferiority, after all better implementation requirements is fundamental.

Guess you like

Origin blog.csdn.net/weixin_34357436/article/details/91365430