JavaScript传播运算符

时间:2018-12-09 14:10:15

标签: javascript ecmascript-6

我想直接在函数的参数列表上使用es6从对象获取这些属性,但我不知道该怎么做:

    function methodA(person){
       var driverName = person.name,
       age = person.age,
       company = person.job.company;
       ...
    }

有没有朝这个方向发展的提示?

1 个答案:

答案 0 :(得分:0)

采取destructuring assignment

function methodA(person) {
    var { name: driverName, age, job: { company } } = person;
    console.log(driverName, age, company);
}

methodA({ name: 'Grace', age: 49, job: { company: 'Infinity' } })