DEV Community

Discussion on: How to access “this” from inside a callback

Collapse
 
reegodev profile image
Matteo Rigon • Edited

Actually there are cases where you want to have the inner scope rather then the outer:


const obj = {
  x: 10,
  foo() {
      return {
        x: 20,
        printX() {
          console.log(this.x); // prints 20
        }
      }
  },
  bar() {
      return {
        x: 20,
        printX: () => console.log(this.x) // prints 10
      }
  },
}

Collapse
 
marklai1998 profile image
Mark Lai

Sorry, this pattern is just worse, really worse
Hope you won't type this in real world