What does the following code output? const obj = { x: 1, getX() { return this.x; } }; const { getX } = obj; console.log(getX());

JavaScript Professional Hard

JavaScript Professional — Hard

What does the following code output? const obj = { x: 1, getX() { return this.x; } }; const { getX } = obj; console.log(getX());

Key points

  • Destructuring does not maintain the original object's context
  • this inside a destructured method does not refer to the object it came from
  • The output is not 1 because this binding is lost in destructuring

Ready to go further?

Related questions