Object deconstruction of javascript ES6

foreword

In the process of communicating with the master, I learned that there is a relatively magical operation in javascript-object deconstruction. I was stunned when I first saw the code and console, but after careful analysis, I got a preliminary understanding of the reasons. I hereby record it for easy reference and share it with everyone.


example one

var {
   
   a} ={
    a: 1,
    b: 2
}

Results of the:
Write picture description here

Example two

var {
   
   a, d=1} ={
    a: 1,
    b: 2
}

Write picture description here

Example three

var {
   
   a, b:{c}, d} = {
    a: 1,
    b: {c: 1},
    d: 2
}
//解构是按照相对路径去查找源对象。在对象中b的值其实是一个对象{c:1},这对应了父对象的b:{c},所以会输出c=1

Write picture description here

Example four

var {
   
   a, b, b:{c}, d} = {
    a: 1,
    b: {c: 1},
    d: 2
}

Write picture description here


Reference

  1. "Easy to understand ES6 (6): Deconstructing Destructuring"
  2. 2.

Guess you like

Origin blog.csdn.net/Jason_first/article/details/79261213