re: How to access “this” from inside a callback VIEW POST

FULL DISCUSSION
 

I use arrow function 100% of time
I think that is much cleaner
Plus I can't think any reason to scope this inside a function (just don't make sense to me, scope in class maybe)

 

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
      }
  },
}

 

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

 
code of conduct - report abuse