ECMAScript 6 arrow functions

后端 未结 3 1043
无人共我
无人共我 2020-12-11 10:12
var getTempItem = id => ({ id: id, name: \"Temp\" });

I know the above arrow function is equivalent to:

var getTempItem = functi         


        
3条回答
  •  时光取名叫无心
    2020-12-11 11:09

    Showing it with an example.

    Parameter Destructuring:

    Here you can see that while logEmployee function is taking in two parameters, we are only passing in the employee object as a part of the code. We are not passing individual parameters. So at runtime the employee object's contents are extracted to match the params that the function is expecting and are passed in accordingly.

    const employee = {
     id: 1,
     name: "John",
     age: 28
    }
    
    const logEmployee = ({name, age}) => (
      console.log(name, age)
    )
    
    logEmployee(employee);
    

    Note that only name and age are required by the function, so only those two properties will be destructured from the employee object.

提交回复
热议问题