I'm confused. How I was able to save a new method to the Test class using this.outer
? I didn't think this
in this.wrap(3)
would have a value at that time.
https://codesandbox.io/s/beautiful-hopper-on7zhs?file=/src/index.js
class Test {
x = 1;
addX(i) {
return i + this.x;
}
wrap(i) {
return () => this.addX(i);
}
howDoesThisWork = this.wrap(3);
}
const t = new Test();
console.log(t.howDoesThisWork()); // prints 4