JavaScript function (a)

I. Overview of functions

  1 Overview

    A relatively independent of the code blocks having a specific function package to form a separate entity, is a function, a name (function name), the subsequent development can repeatedly call action function is to package a piece of code can be reused in the future.

  2、

  3、

Second, use the function

  1, the definition of the function

    a, function declarations

     Syntax :

function name of the function () { 
  // function body 
}

    b, function expression

     Syntax :

function Fn = var () { 
  // function body 
}

      Note :

      •  Only defined functions and will not be executed, only when the function is called, will be performed;
      •    Naming function using verb + noun format, it does indicate one thing.

  2, call the function

    Syntax:

Function name();

         Note : the function body will only be performed when called, can be called multiple times, re-use

  3, parameters of the function

    Internal function is a closed environment, may pass outside through the function value of the internal parameter.

    Function declaration syntax with parameters :

function function name (parameter 1, parameter 2, parameter ...) { 
  // function body 
}

     Function arguments call syntax:

Function name (argument 1, argument 2, argument 3);

     a, arguments between Form

     Formal parameters: when declaring a function, in order to function functions more flexible, some values ​​are not fixed, and can not be fixed for these values. We can set parameters to a function. This parameter is no specific value, accounting for position only play a role, we usually call the formal parameters, also called parameters.

     The actual parameters: If the function when you declare, set the parameter, then the function call when you need to pass the corresponding parameters, we passed parameter called the actual parameters, also called the argument.

      b、

    Extended : When an incoming parameters for basic data types, changing the value of the parameter within the function, and does not affect the value of the external argument.

  4, the return value of the function

    When function execution is completed, the desired feedback function for some subsequent operation, this time the return value of a function is required.

    a, the return value Syntax

// declare a return value of the function 
function function name (parameter 1, parameter 2, parameter ...) { 
  // function body 
  return value return; 
} 

// the return value may be received via the variable 
var = variable function name (argument 1, argument 2, argument 3);

    b, Note

      •  If the function does not use the return statement is displayed, then the function has a default return value: undefined
      •    If the function using the return statement, then back again with the return value, it becomes a function's return value
      •    If the function using the return statement, but did not return back any values, the return value is also a function of: undefined
      •    After a few use the return statement, the function will be stopped after the completion of the implementation of a return statement and exit immediately, which means that all other code will not return back to perform again.

    c、

Three, arguments

  In JavaScript, arguments object is an object is rather special, in fact, is currently a built-in properties of the function. That all functions built-in objects arguments, arguments object stores all the arguments passed. It is a dummy arguments array, and thus may be traversed.

  Demo:

 1   // 求任意个数的和
 2     function getSum() {
 3       var sum = 0;
 4       for (var i = 0; i < arguments.length; i++) {
 5         sum += arguments[i];
 6       }
 7       return sum;
 8     }
 9 
10     var sum = getSum(5, 1, 3, 4);
11     console.log(sum);

 

四、

Guess you like

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