Javascript basis - function

Introduction of a function

  1. The concept: a bunch of code in accordance with certain rules, make up to achieve a certain function, call the function itself or by event

   Function features:

    Ignore the details: only concerned with the function of its implementation, does not care about the internal structure;

    Reuse: function code reuse may be implemented;

    Choose to use: only when the function is executed, the function code to run, do not perform that is not running.

    Principles defined functions: a function package now also need to have the external interface for external information and interactive functions
 
  2. Create (definitions, declarations) function:
       Declarative: Keyword function key, the function declaration, similar to var
    For example: function fn () {} (function declaration keyword; Fn function name; () external interface parameters; {} case statement execution, the function body function)
 
    Assignment type: declare a variable, the function assigned to the variable; the variable name that is the function name
    E.g. var fn; fn = function () {}
  3. Functions of the Executive:
     Their execution: function name (), in any case, as long as the function name (), the function is immediately executed (active execution)
     Behavior (event) call: an element binding event which triggers that the implementation (passive execution)
      The element event = function name ------> box.onclick = fn;
         Element. Event = SUMMARY ------> box.onclick = function () {} function
  4. The arguments (argument between Form)
    Arguments: performing parameter called arguments, the argument is received parameter is a specific value;
    Parameter: parameter definition called parameter, parameter save the argument, the parameter is a variable.
      (Parameter passing: a behavior, the arguments passed to the function executed when the parameter)
function fn(a){
     console.log(a);    //5
}
fn(5);

    Number of parameters: You can pass an unlimited number.

    Parameter Type: can be any data type. (When a function as a parameter A, B, passed to another function, is performed as a function of the A parameter, called callback)

    Relationship: arguments and parameters, consistent with the number, in accordance with the order of one to one (multi-parameter, multi-parameter out is undefined; multiple arguments, the extra argument, arguments are passed to the object)

function fn(b,a,c){
     console.log(a);                //world
     console.log(b);                //hello       
     console.log(c);                //123
     console.log(argument);       //Argument{0:"hello",1:"world",2:123,3:456,4:789}
}
fn("hello","world",123,456,789) ;   

    argument: is a dummy array having a length and an index; can get by traversing all the arguments.

  The return value of the function  

    Return Value: after the function is executed, the data obtained, execution returns to the statement function.

       (By default, the function returns undefined, the return value if there is need to use the keyword in a function: return)

    Return Value effect: if the data after the data handler, processing, may need to use the second, the need for a return value and returns processed data
    
The scope of two functions
  1. Concept: variable region of the entry into force
 
       2. Regional: global and local
     Global: The entire code file, does not belong to any of these functions is global (variables in the global scope, called a global variable)
     Local: Each function is a local (variable in local scope, called a local variable)
 
  3. Scope access access rules:
             (1) can not cross the same level of local
             (2) the parent can not take the child, the child could get Father
             (3) more parent-child class scope have to find the nearest Scope
Application of three functions
  1. to enhance the variables: variables var declared in advance to the starting position of the current scope, is declared. In situ assignment.
 
  2. enhance the function: All function using the function statement, will enhance the overall, long declares a function in the current scope, can be used in any position of the current action
        (Note: Assignment type 1 to create a function to enhance a statement, not a function
            2. When the functions and variables the same name, higher variable enhance, improve the function of the following variables, resulting in the function to take effect)
function fn(){
    console.log(a);     //f2
    var a = "hello";
    console.log(a);     //"hello"
    function a(){1}
    console.log(a);     //"hello"
    a = "world";
    console.log(a);     //"world"
    function a(){2}
    console.log(a);     //"world"
}
fn();
// This code is equivalent to ↓↓↓ 
function Fn () {
     var A;
     function A (). 1 { }
     function A () {2 }
    console.log(a);     //f2
    a = "hello";
    console.log(a);     //"hello"
    console.log(a);     //"hello"
    a = "world";
    console.log(a);     //"world"
    console.log(a);     //"world"
}
fn();

  3. recursive functions: the implementation of their own in the function (recursive most important thing is to stop condition)

 // factorial 10 recursive 
function Fn (n-) {
     IF (n-==. 1 ) {
         return . 1 
    } the else {
         return n-Fn * (n--. 1 )
    }
}
console.log(fn(10))        
// use recursion Fibonacci number (1,1,2,3,5,8,13,21,34,55,89 ...)
 function Fn (n-) {
     IF (n-==. 1 || 2 == n- ) {
         return . 1  
    } the else {
         return Fn (. 1-n-) + Fn (2-n- )
    }
}
console.log(fn(5))                

  4. Constructor

    Constructors: not a function is a way of creating a data function to perform.

    Keywords: new (used to perform the function)
// create a numeric data: 
var n-= new new Number The (123 );
 // Create character data: 
var S = new new String ( "Hello");

 



 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 


 
   
 

Guess you like

Origin www.cnblogs.com/miaomiaolong2/p/12002071.html