Recent situation: has been busy to find internships have not been updated, but the study is still continuing. I recently wrote node. JS and picked up the JavaScript corner for half a month to see Python and some deep CSS books and blogs. Work to find the good, took two internship offer, decided to go when the small two. These days in a look at the teacher's "JavaScript Framework Design", the first to talk about the seed module (that is, the core module) mentioned a not seen in the abbreviation Iife, checked, looked at.
1. What is Iife
Iife is an abbreviated, fully spelled imdiately invoked function expression that executes immediately.
It may be easy to think of this syntax at this time:
(function() { //...}) ();
is an immediate execution statement of an anonymous function that we usually write.
Iife is this, but more than that, there are several other statement formats: +,-, ~,! , void.
+function(){ //Code 1}();-function(){ //Code 2}();!function(){ //Code 3}();~function(){ //Code 4}();void function(){ //Code 5}();(function(){ //Code 6})();
Although the above functions are of different formats, the function is the same: the anonymous function executes immediately.
One of the last (function () {//...}) (), before learning JS, when there is a book called () is a mandatory operator. In fact, JS does not have the concept of mandatory operators, the first pair of parentheses () is wrapped in function outside the role of the actual is to the JS interpreter | | The engine provides a parsing scheme that prompts the interpreter to include a whole content inside the parentheses. The effect of the second parenthesis is that the function call executes, but if directly through function () {//...} () or function A () {//...} (); it cannot be performed because the engine defaults to the function declaration when it encounters the functions () {} structure, and the last Face () is parsed separately, and then the syntax is given an error.
The first several Iife statements are also converted to function expressions by the unary operator +-~!, which is intended to prompt the JS parser to tell the JS interpreter that the operator is near an expression.
function A () { //... }(); // the above notation is interpreted as equivalent to function A () {};(); // Error
The Iife method is compared with the normal first declaration and then the following features:
1. Implementation is immediate and does not need to be declared before being called.
2. The anonymous function itself does not pollute the global environment, while providing internal variables to the environment space. (The mainstream framework is now used to initialize namespaces in this way.)
3. Abbreviated parameters facilitate internal code compression
4. Provide a closure environment, you can do what the closures want to do
(function(w,$) { // internal Call to Window object and JQuery object, can be used directly with W and $ Access }) (Window,jquery) ;
Also, by passing in global objects, you can make it faster to find variables on the scope chain.
But there is a "no problem" problem in this way, and I've seen it on the internet to suggest that this approach will cause poor code readability.
Although this execution looks simple, it is the executing function of the seed module for almost any framework. (The seed module is the first part of the framework, and it is the core module that the framework can be used for, in terms of functionality.) The primary role of the seed module is to create namespaces, build base objects, provide function extensions, event bindings, conflict handling, and so on. Iife is the "init function" that the framework is self-executing. )
2.IIFE differences in several statement forms
It doesn't make any difference, but I've seen some of the blog's performance comparisons. Found the original site of the test http://jsperf.com/iife-different-operator-efficiency, ran a bit, the result.
This machine is a Mac, using the Chrome41 browser. void format works best, and the worst + direct difference is 20%. + already very efficient, so I think there is no need to care about the difference, after all, it is very rare to call the Iife format of the function. Then ran again, found that the value is in the micro-dynamic changes. So the specific use of which is to see the habit.
Here are some of the formats used by the mainstream framework:
jQuery1.10 (function () {//...}) (window);
BOOTSTRAP3 + (function (a) {"Use strict";//...}) (JQuery);
Amazeui! (function () {//...})
Zepto (function () {//...}) ();
Iife can write so much, not too much dry, readily remember a lively.
[JavaScript] iife function expression immediately executed