Object destructuring with property names that are not valid variable names

前端 未结 2 812
轻奢々
轻奢々 2020-11-27 07:31

Does anyone know if you can use object destructuring with spaces in the property name? Maybe this cannot be done and I realize the JavaScript notation is incorrect but I can

2条回答
  •  一向
    一向 (楼主)
    2020-11-27 07:50

    You can assign it a valid variable name using this syntax:

    var {"my name": myName, age} = obj2; 
    
    // use myName here
    

提交回复
热议问题