Arrow function parameters deconstruction

var Elements = [
   'Hydrogen' ,
   'Helium' ,
   'Lithium' ,
   'Beryllium' 
]; 

elements.map ( function (Element) { 
   return element.length; 
}); // Returns an array: [8, 6, 7, 9] 

// the above can be rewritten as ordinary functions arrow function 
elements.map ((Element) => {
   return element.length; 
}); // [. 8,. 6,. 7, 9] 

// when only the function of the arrow parentheses a parameter, the parameter may be omitted 
elements.map (Element => {
  return element.length; 
}); // [. 8,. 6,. 7,. 9] 

//When the function is only a function of the body of the arrow `return` statement, the braces can be omitted` return` keywords and method body 
elements.map (Element => element.length); // [. 8,. 6,. 7,. 9] 

// in this example, because we only need `length` property, you can use parameters deconstructed 
// Note that the string` "length" `is the name of the property we want to get, and then just` lengthFooBArX` a variable name, 
// can be replaced with any valid variable names 
elements.map (({ "length": lengthFooBArX}) => lengthFooBArX); // [. 8,. 6,. 7,. 9]

 

Guess you like

Origin www.cnblogs.com/wang715100018066/p/11268532.html