What is destructuring assignment and its uses?

后端 未结 3 759
深忆病人
深忆病人 2020-11-22 00:28

I have been reading about Destructuring assignment introduced in ES6.

What is the purpose of this syntax, why was it introduced, and what are some examples of how it

3条回答
  •  执念已碎
    2020-11-22 01:22

    It is something like what you have can be extracted with the same variable name

    The destructuring assignment is a JavaScript expression that makes it possible to unpack values from arrays or properties from objects into distinct variables. Let's get the month values from an array using destructuring assignment

    var [one, two, three] = ['orange', 'mango', 'banana'];
    
    console.log(one); // "orange"
    console.log(two); // "mango"
    console.log(three); // "banana"
    

    and you can get user properties of an object using destructuring assignment,

    var {name, age} = {name: 'John', age: 32};
    
    console.log(name); // John
    console.log(age); // 32
    

提交回复
热议问题