Array of method calls?

AFAIK, a JS method is a function that relies on the keyword this:

When we call a function, its current internal this becomes equal to the object of the left side of the dot . accessor operator.

However, if we assign a function reference to a variable or container, we end up calling that function w/o the dot . operator; thus its this is undefined!

As a workaround, we can call such methods via call(), so we can choose its this:

For a more permanent solution, we can call bind() at the moment we’re assigning a method to a variable or container, so its this is forever bound to a specific object:

2 Likes