What's the difference between object destructuring and normal object assignment in Javascript ES6?

后端 未结 2 1287
孤街浪徒
孤街浪徒 2021-01-12 21:38

What\'s the difference between this two code examples (besides the syntax of course)?

EXAMPLE 1:

var user = {
   name: \'Diego\',
   age: 25
}

var         


        
2条回答
  •  不知归路
    2021-01-12 22:06

    There's no effective difference, but destructuring is convenient:

    var user = {
       name: 'Diego',
       age: 25
    }
    
    var {name, age} = user;
    

    That declares and initializes both name and age in one statement without redundant mentions of the property names.

提交回复
热议问题