Losing This
JavaScript doesn’t have bound method and we can easily lose this
when we store or pass a method as a variable. We can use bind
to fix this:
const ron = new Person("Ron");
const greet = ron.greet;
greet(); // Oops, this is undefined
const greet2 = ron.greet.bind(ron);
greet2(); // ok
When writing our own classes, we can even add some boilerplate code to automate that.