JS function declaration function function callback function three elements arrow

function

  You can achieve a certain piece of code function package.

Note: After the function to create, in the heap stored in a string of text, will not have any effect is not performed, it just saved a string.

Creating function

  1, literal (recommended)
 
      Key identifier parentheses {} function body
 
    Example:
function F65(){
    console.log ( `I was a function F65`);
 }

   

  2, function expressions (anonymous function expression)
 
      Key identifier = function () {} function body
    Example:
let F66 = function () {
    console.log ( `I was a function F66`);
}
let F66 = function F67() {
    console.log ( `I was a function F66`);
}
F66 (); // I is a function F66 
console.log (F66.name); // F67 
console.log (F66); // [Function: F67] 
// F67 (); // error F67 is not defined 
// this situation is a bit wonderful
 function F67() {
            console.log ( `I was a function F67`);
        }
  F67 (); // I'm an F67 function
// means that the front and did not declare the variable F67, F67 only just in front of the name as a function, but Shique call must use F66 as a function name, lame
 

 

  3, constructors
 
        Key identifier = new Function ();
    Example:
let F68= new Function(
    "console.log(123)"
);

Note: Memory Stack F68: reference to the function;

  Heap Memory Function: "console.log (123)", is a string.

 

A function of three elements

  First, the function name

    Function name is the name generally require a verb or action Binger formula, look EENOW text, and the other as variable identifiers, and also, and variable names.

  

  Second, the parameters

      Parameter: When you create a function, add the parameter
      Arguments: the function is called, add the parameter
NOTE: Participants shaped initial value: undefined.
 
     The function is called, the arguments passed to the process parameter corresponds to:
        let = parameter argument;
     Several parameters, corresponding to a few let.
 
     When the real number of different parameters involved in shape, in a conventional programming language, the number being given different, i.e., it is not performed. JS will not:
  
          Shape parameter greater than the real number more than the number of reference parameter, value is undefined
  
          The number of parameters is larger than the number of real-shaped multi-parameter argument is not used
 
  Extended:
      1, at the end of the extended operation adding variable (argument acquired more) Array
       It must be written in the last parameter position (Ogata Senate EDITORIAL will complain), and only one
  
      2, arguments acquires the content of all the arguments (Array class)
  
        Class array:
          1, can take the values ​​with the subscript arguments [0]
          2, can be used to get the length length (number)
          3, but the method can not be an array, for example, push () being given
  
  Note: The name of the function parameter is taken .length number (does not include extended operator identifier).
function F65(a,b,c,...d){

    the console.log (A, B, C); // the ABC 
    the console.log (D); // [ 'D', 'E', 'F.', 'G', 'H'] 
    the console.log (F65. length); // . 3 
    the console.log (F65.arguments); // key arguments of 
    the console.log (F65.arguments [0]); // a 
}
F65("A","B","C","D","E","F","G","H");

 

function f65(x,y,z,i){
     console.log(arguments[0]);//1
     x=2;
     console.log(arguments[0]);//2
     arguments[0]=5;
     the console.log (X); // . 5 
     // changed parameter value, corresponding arguments also changed 
     // value change occurs arguments, parameters corresponding to the shape does not change 
     console.log (i) ; // UN 
     I = 2 ;
     the console.log (I); // 2 
     the console.log (arguments [. 3]); // UN 
     // When parameter argument than long, more formal parameters of the assignment; 
     // corresponding arguments does not change ( That is invalid, that is, print undefined) 
}
 f65(1,2,3);

Note: Function names .arguments and direct arguments are not the same, in fact, are generally directly arguments.

  

  Third, the return value: return

    
    Each function must have a return, if not add a return statement, the computer will automatically make a return in the last line of the function, the return value is undefined.
    Function returns a result (constants, variables, equations, expressions), and termination functions, i.e., the code will not be executed later return.

 

 The default value of the parameter

  Shape function parameters can be directly assigned at the time of writing (there will be no assignment: undefined), when the function is called, if there is a corresponding parameter arguments, the argument value to use, not to use the default values.

  To pass parameter values: undefined, functions will use the default value of the parameter (not necessarily undefined),

  When the number of parameter acquiring .length, tangible default values ​​assigned reference number is not included, a default value if the assigned shape is a parameter written in the length will be zero.

  Therefore, it is recommended the need to assign the default parameter values ​​into the last.

 

 ES6 arrow function

    

        = the X-> num2.has (the X-) returns the expression most of the time (on this template, function num2.has (x)) results
 

  Generally have the following characteristics:

        1, can be omitted writing function

        2, if there is only one parameter, you can not fight parentheses

        3. If only one execution statement, you can not fight braces

        4, only one execution statement, and this statement needs to return a result, you can not write return

        5, can not use arguments

        6, this will not be bound to their function

        7, not be used as a constructor

  Most of the time using the arrow function only one statement, is to simple and convenient:

let num1 = new Set([1, 2, 3, 4]);
let num2 = new Set([3, 4, 5, 6]);
// 并集
let union = new Set([...num1,...num2]);
console.log(union);//Set { 1, 2, 3, 4, 5, 6 }
//交集
let intersect = new Set(
    [...num1].filter(x=> num2.has(x)));
console.log(intersect); //Set { 3, 4 }
//差集
let difference = new Set(
    [...num1].filter(x => !num2.has(x)));
console.log(difference); //Set { 1, 2 }

 

Callback

   A to a function, when passing parameters to call another function B, function A is the callback function.

    The arrow up to function as a callback function used.

  Common callback function

    

// sorted array of 
the let ARR = [. 1,. 3,. 7,. 6,. 9,. 4,. 6, 2 ];
let arr1 = arr.sort(
    function (a,b){
        // return b-a;
        return a-b;
    }
)
console.log(arr1);

the console.log (arr.sort ((A, B) => A - B)); // ascending 
the console.log (arr.sort ((A, B) => B - A)); // descending

  The callback function with a very large, very important.

arr.every(
    function(index){
        return index%2==0;
    }
) // The following is the abbreviated
// Every each value in the array satisfies the condition before returning true 
the console.log (arr.every (index => index% 2 == 0)); // to false 
// some conditions array has a true return 
console .log (arr.some (index => index% 2 == 0)); // to true 
// filter (filtering) returns all values that satisfy the condition 
console.log (arr.filter (index => index % 2 == 0)); // [6,6,4,2] 
// Map each value satisfies the condition return 
the console.log (arr.map (index => index% 2 == 0)); // [to false, false, true, true, true, false, true, false]

 

IIFE

  Immediately Invoked Function Expression

     Immediate execution function, namely to create a call, the call after destruction

Note: When performing an immediate function of variables out there called, the variable will not be destroyed, but will be "invisible", calling it only "people" see.

// general notation 
( function Fn () {
    console.log("F65");
})()
// may be 
( function Fn () {
    console.log("F65");
}())

!(function fn(){
    console.log("F65");
}())

-(function fn(){
    console.log("F65");
}())
// mainly in order to make this piece of code is separated from the other code, no ambiguity, to form a whole

 

Guess you like

Origin www.cnblogs.com/jiayouba/p/11954584.html