Function declarations, function expressions compared with the immediately called function expressions

  

  Function declarations

     Function declaration code calls the function to create the future. Function can be called before the position statement. Sample code is as follows:

  

  1. // can be called before the position statement
  2. var size=area(3,6);
  3. function area(width,height){
  4. return width*height;
  5. };
  6. // can be called in position after the statement
  7. var size2=area(2,4);
     

    Function expression

      The expression of the function to be present in a location, which is referred to as function expression. In the function expression often used anonymous functions. The following code sample

      

    Area function = var (width, height) {
    return width * height;
    };
    // can only be invoked after the function expression is defined
    var size = area (2,4);
    immediately invoke function expression (IIFE)

       No name, execute one time when the interpreter through them. Sample code is as follows:

       

    var area=(function(){
    var width=6;
    var height=8;
    return width*height;
    }());

Guess you like

Origin www.cnblogs.com/lvfang/p/11016556.html