this keyword and it’s behavior in both languages certainly lends some merit to that argument. Most modern programming languages like Java have some version of a
class and within those
this is decidedly different when compared to other modern languages.
window object in a browser or even the
global object in Node.js.
this. One of the more popular ways is to use ES6’s arrow functions. These functions upon compiling, retain the
this value of the enclosing lexical scope. Another strategy is to use the
bind() method to explicitly set the value of a method’s
this to it’s enclosing lexical scope regardless of how/when it’s called. The last strategy that I’m aware of is simply setting a function as a method of an object — when you do that it’s
this is bound to that object.
Long story short, I hope I’ve provided a quick and helpful explanation surrounding the weirdness of
this and hopefully these are some useful workarounds that you can easily understand and employ. Cheers!