Implement a function instance in JavaScript

JS Implementing a function allows your function to execute immediately after you created, JS immediately performs a function mode is a syntax, you can make your function immediately after defining Executed, this model is essentially a function expression (named or anonymous), executed immediately after being created.

(function () {…}) () and (Function () {…} ()) are common writes for two JavaScript immediate execution functions. I thought it was a parentheses package anonymous function, and then After adding a parentheses calling function, finally perform the purpose of executing immediately after the function is reached, and then it is not the case.

Don’t say more below, let’s take a look at the detailed introduction.

Usually, we declare that a function has the following ways:

// Declaration function F1Function F1 () {Console.log (“f1”);} // Pass () to call this function f1 (); // an anonymous function function expression, assigned to the variable F2: var f2 = function () {Console.log (“f2” );} // Pass () to call this function F2 (), a function expression of a function named F3 (the function name here can be named at will, you can assign the variable to the variable) F3: var f3 = function f3 () {console.log (“f3”);} // Pass () to call this function F3 ();
   if you If you have seen some custom controls, you will find that most of them are using this way: 

(Function () {“ “ 开始 写 写 功能 需}) ();
  This is what we often sayImmediate execution function (iife), as the name suggest, that is, this function is to perform a function body, do not need you to take an extractive to call, in general, we only use IIFE for anonymous functions, there are two purposes:  
First, it is not necessary to name the function, avoiding pollution global variables

The IIFE has formed a separate scope, which can encapsulate some externally unread private variables.

If you see these two sentences, you can’t understand it, then start from the IIFE’s operating principle.

Because IIFE is usually used for anonymous functions, here is a simple anonymous function as a chestnut:

 VAR f = function () {Console .log ("f");} f ();   We found that f is just a reference variable of this anonymous function, then since f () can call this function , I will replace the FRED function itself? 

Function () {console.log (“f”);} ();

uncaught syntaxerror: unexpected token (

 The reason for generating this error is that the JavaScript engine sees the function keyword, it is considered that the function declaration statement is the function declaration statement. The solution is to let the engine know that the front part of the parentheses is not a function definition statement, and It is an expression that can be calculated here. This is distinguished from the function declaration and function expression:   1. Function declaration (ie we usually use function x () {} to declare a function)

function myFunction () {/ * logic here * /}

 2, function expression (similar in this form)   

var myFunction = function () {/ * logic here * /}; var myObj = {myFunction: function () {/ * logic here * /}};

 Primary we learned through the use () expression enclosed first performs, as follows:   
1 + (2 + 3) // content to run inside the parentheses there are no comments Caesar

 in fact, small javascript brackets also have a similar effect, see the function keyword Javascript engine would think that is a function declaration statement, if priority Javascript engine to see what will happen parentheses:   
// with parentheses to wrap function (function () {console.log ( “f”);}) ();

 function successfully performed:   
f // console output

 In this case Javascript engine will think this is an expression rather than function statement, of course, make the Javascript engine that this is a way of expression of many:!   
function () {} (); + function () {} (); – function () {} (); ~ function () {} (); new function () {/ * code * /} new function () {/ * code * / } () // Only the last parentheses are required only when the parameters are passed. …

Back to the previous question, why said this form avoids pollution global variables, if you have seen the jQuery plugin written by others, there is usually similar Such code:
   (function) ($) {`` `// plugin implementation code} (jQuery); 

The jQuery here is actually the parameters of the anonymous function. When we call an anonymous function, it is f () so anonymous with parameters, which is F (Args), here, introducing jQuery as a parameter into the function, Then use shape parameters in the function, it will not affect the external environment, because some plugins use $ this qualifier, you can toss in this function.
 In this process, the following two articles are referred to:   JavaScript Implements a function: in the plugin in function () {} () Reflections 

in JavaScript
The above is all of this article, I hope this article There must be a certain reference value for everyone’s learning or work. If you have any questions, you can leave a message, thank you for your support of Tumi Cloud.

© Copyright Notice
Just support it if you like
comment Grab the couch

Please log in to comment