Deconstruction of ES6- function

clipboard.png

The deconstruction of the

function does not quite understand the red arrows in this. Why undefined? Is it not true that when let {XMagi y} = {XRO 0PersonyGl} , x can output equal to 0LI YQ 1, although XMagi y is a pattern and the assignment is assigned to the XMagi y variable, then why is it not [0J 0] here?

2. And Mr. Ruan said that the default value only works when it is equal to undefined, then let {XRV 1Magi y Magi 2} = {}; x will be equal to 1Magi y and y will be equal to 2. Is it because there is no XMague y attribute set on the right, so there is no xMagy on the right, so both undefined,x equals 1Magney equals 2? Is that how you understand it?

Mar.04,2021

look at move ({x: 3}) , where the y of the {x: 3} object is undefined ,
so the result is [3, undefined]

notice the difference between the second move and the first move,
in the first move:

function move({x = 0, y = 0} = {}) {
  return [x, y];
}
// 
function move(a) {
  if (a === undefined) {
    a = {}
  }

  let x = a.x
  if (x === undefined) {
    x = 0
  }
  let y = a.y
  if (y === undefined) {
    y = 0
  }

  return [x, y]
}

and the second move is equivalent to:

function move(a = {x: 0, y: 0}) {
  return [a.x, a.y];
}

// 
function move(a) {
  if (a === undefined) {
    a = {x: 0, y: 0}
  }

  //  a.x  a.y 
  return [a.x, a.y];
}

that is, when an is undefined , a takes the default value {x: 0, y: 0} .


you can look at it from the equal sign of the structure:

function move({x, y} = { x: 0, y: 0}) {...}

here the left part of the equal sign is treated as a whole to assign the default value, and its only input value is in the second move in the question.
if there is an input, the input value is structured, and if no input is entered, the default value is used.

notice that there are three equal signs in the front move and only one equal sign in the move

You have been answered by

upstairs. This is a way to assign default values. It is recommended to read through the ES6 document


. I happen to see this block today. The first function contains the default values of function parameters and object deconstruction, while the second function is only the default values of function parameters.

Menu