JS self-executing function

  Js because of their weak fundamentals, a lot of js knowledge has not mastered, so the next blog will often write about js basic knowledge, it can be considered to own publicity department.

  js self-executing functions, heard the name, first of all think of the function. Next, I'll define a function:

function aaa(a,b){
    return sum = a + b
}

  It defines a function named aaa, which can be calculated in two numbers. If you want to do it, you have to call it, and it had to pass parameters:

the var 'aa aaa for = (1,2)

  This realization of the definition of a function call, we can see through console.log sum realized the sum of the two numbers.

  What functions are self-executing? Self-executing function when the function is defined it is out, it will automatically be executed. No need to call, mass participation is also very convenient. On the above function, the function definition with the implementation of self is this:

(function aaa(a,b){
    return sum = a + b
;})(1,2)

  Console can be found by adding the sum of the two numbers to achieve.

  There are three self-executing functions written:

  1.   (Function ( "parameter") { "function method";}) ( "passed to the parameter value")
  2.   (Function ( "parameter") { "function method";} ( "pass to the parameter value"))
  3.        ! Function ( "parameter") { "function method";} ( "pass to the parameter value") 

  A third! Can be exchanged for other operators or void.

  Self-executing function is very selfish, its interior can access the global variables. But in addition to its own internal self-executing function, it is unable to access it. example:

function aaa(a1,b1){
    return sum1 = a1 + b1
},
(function bbb(a2,b2){
    return sum2 = a2 + b2
;}(),
console.log(aaa)
console.log(bbb)

  This is a function of a self-executing function, the output of these two functions will find: aaa function is completely printed, and bbb is an error. Since performing a function corresponding to the bottle down cup, when defining it, it will tilt the cup is exposed, absorb fresh air from outside; when it is finished, the cup is no longer exposed, closed up, no longer associated with the outside world.

Guess you like

Origin www.cnblogs.com/btlbk/p/11111025.html