JavaScript function (b)

First, the anonymous function

  1, an anonymous function

      Function without a name that is known as an anonymous function.

  2, using the method

      a, the anonymous function is assigned to a variable, so that you can call by variable

      b, from anonymous function calls

  3, since the effect on execution of the function (function from anonymous calls): The global variables to prevent contamination.

  Demo:

1  // 1 function declarations - named function 
2  function Fn () {
 . 3     // function body 
4  }
 . 5  
. 6  // 2 function expressions - the second half of the anonymous function 
. 7  var Fn = function () {
 . 8      // function body     
9  }
 10  
. 11  @ 3 from the calling function - after completion of the function call immediately written 
12 is   ( function () {
 13 is      the console.log ( 'I am calling the function from' );
 14   }) ()

 

Second, since the calling function

  Anonymous calling function can not be performed directly, and therefore may be performed by way of self-called anonymous function, as described in Example 3.

Third, the function is a data type

  1, the function is a data type

function fn() {}
console.log(typeof fn);         // function

    From the data type of a function of the type obtained, the function is a kind of Object belonging to a reference data type.

  2, as a function of the parameter

    As a function of a data type, it can function as an argument of a function, a function call.

  3, the return value as a function of

    As a function of a data type, it can be returned as a return value from a function of an internal function.

    Demo:

1  // as a function of the parameter 
2  var Fn = function () {
 . 3        the console.log ( 'the Hello World' );
 . 4  }
 . 5  function test1 (Fun) {
 . 6     Fun ();  
 . 7  }
 . 8  test1 (Fn); // pass into a function
 . 9  
10  // function as a return value 
. 11  function test2 (a) {
 12 is    var B =. 5 ;
 13 is  
14     return  function () {
 15        the console.log (a + B);
 16     }
 . 17  }
18 is  var Fn = test2 (. 6 ); // return a function
 . 19 Fn ();

 

four,

Fives,

 

Guess you like

Origin www.cnblogs.com/niujifei/p/11333892.html