Creating variables for destructured objects in addition to their properties

后端 未结 3 1958
我寻月下人不归
我寻月下人不归 2021-01-17 11:33

I have:

const { state: { mode } } = this

console.log(mode) //\'mode\'
console.log(state) //undefined

I want to declare the state

3条回答
  •  北海茫月
    2021-01-17 12:06

    While all the other answer here suggested a word around to get values, but i am adding this answer to explain why we getting only the deep most nested value

    let state = {
        state: {
          mode : 'some value'
     }
    }
    
    const { state: { mode } } = state
    

    When you do nested destructing you will be complied to something like this

    var state = {
      state: {
        mode: 'some value'
      }
    };
    var mode = state.state.mode;  // this is how your de-structuring is interpreted 
    

    It is not creating a separate variable for each of the property,

提交回复
热议问题