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
function F65(){ console.log ( `I was a function F65`); }
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
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
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
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