0

Is it possible to do something like the following in js?

function Something( Point{PointX, PointY}   ) {
    console.log('Point', Point);
    console.log('PointX,PointY', PointX, PointY);
}
Something([1,2]);

If so, what would be the proper way to do that?

David542
  • 104,438
  • 178
  • 489
  • 842
  • You either destructure or you don't. If you want the whole object, then that should be the parameter, and then destructure in a `let` or `const` inside the function. – Pointy Mar 14 '22 at 21:38

3 Answers3

1
function Something(Point) {
  const [PointX, PointY] = Point
  console.log('Point', Point)
  console.log('PointX,PointY', PointX, PointY)
}
Aaron Vasilev
  • 438
  • 2
  • 6
0

Is this what you need?

function Something([PointX, PointY]) {
    console.log('PointX,PointY', PointX, PointY);
}

You can destructure both arrays (function([x, y]) and objects (function({ x, y })).

twharmon
  • 4,153
  • 5
  • 22
  • 48
0

Sure, but make sure the keys match up to do the destructuring assignment for an object, such as:

function Something({x,y}) {
    console.log('PointX, PointY', x, y);
}
Something({x: 1, y: 2});

In terms on naming and also assigning it to the outer object, perhaps you can do something like this instead:

function Something(Point) {
    const {x,y} = Point;
    console.log('Point, PointX, PointY', Point, x, y);
}
Something({x: 1, y: 2});
David542
  • 104,438
  • 178
  • 489
  • 842