3, the default value functions and function arrows

ES6 using the 'arrows' (=>) defined function
var f = v => v;

1, the arrow function characteristic

  The scope where 1) the value of this function in vivo, this binding is defined

  2) not as constructor

  3) not use the arguments object, (... rest may be used instead)

2, the syntax: () => {}

  Deformation: 1) does not pass parameters, return value codes only

var add = ()=> 2;

console.log(add());

    2) pass a parameter

var add = a => a+2;

console.log(add(a));

    3) pass a plurality of values

var add = (a,b) => a+b;

console.log(add(1,2));

    4) If the object is returned, it is necessary to use () wrapping

var add = (a,b) => ({num:a+b});

console.log(add(10,10));

    5) when the logic function in vivo, we must increase the parentheses, and manually return the desired value of the return

var add = (a,b) =>{
    if(a<b){
        return a;
    }
};

console.log(add(1,10));

3, property analysis

es5 internal functions defined in this is certain, oh when you call, and the arrow in the function of this is to determine when the definition of the scope of the definition of where the binding of this

= the document.onclick () => { 
    the setTimeout (() => { 
       the console.log ( the this ); 
    }     
    , 1000 ) 
     the console.log ( the this ); 
} 
// two window are printed

4, the function expansion

  The default value of the function, to direct assignment parameter, and the function can not be used in vivo or let Again const, being given

// es5 achieve default 
function
Fn (A) { A = A || 10; // this case not insured the console.log (A); } Fn ( 0); // 10 Fn ( ''); // 10

// for ES6 inside the body can not function and parameter variables redeclaration
function Fn (A = 10) {
  the console.log (A)
}
Fn (0);


  rest parameter, rest parameter (in the form of variable names ...), a parameter obtaining extra functions, arguments directly on the array may be substituted arguments, arguments is a collection of arguments (array type), while the rest is an array of arguments

Fn function () { 
  the console.log (arguments);
}
Fn (1,2,3,4);

// parameter REST
function Fn (... arr) {
  the console.log (arr); // this time is arr real array
}
the Fn (, 2, 3)

// can be separated
Fn function (A, B, ... arr) { 
  the console.log (A);
  the console.log (B);
  // this time is the real array arr; the console.log (arr)
}

5, object extension

  1) variable shape and attribute names at the same time, the variable name may be written directly

        2) The function key function can be omitted

  3)Object.assign()

    For merging the object, will be enumerated property copy source object to the target object

var licanv = 10 ;
 var obj = { 
  // lincanv: lincanv     
  lincanv // attribute names and variable names as they can be so abbreviated
   } the console.log (obj)

 Generally for use when writing function modules

var util = (function (){
    function add(){};
    function isFunction(){};
    function isArray(){};
    return {
       add,
       isFunction,
       isArray
    }
})();

util.add()

When defining the function, you can be abbreviated objects directly within the write function

var obj = {
  // fn:function(){}
  function(){}  
}

Object.assign () method

var O = {A:. 1 }
 var O2 = {B: 2 }
 var O3 C = {:. 3 } 

// Copy o2, o3 properties to the o, with both operating in for 
// ES6 use ASSIGN 
Object .assign (O, O1, O2); 
the console.log (O)

Usually Object.assign package widget

function Fn (Option) { 
   the let Defaults = { 
       A: 'default. 1' , 
       B: 'default 2' , 
       C: 'default. 3' 
   } 
    Object.assign (Defaults, Option); 
    the console.log (Defaults); 
} 

// optional parameter passed 
Fn ({ 
  A: . 1 , 
  B: 2 , 
  C: . 3 
})    

 

Guess you like

Origin www.cnblogs.com/gopark/p/11357122.html