JavaScript: Why was dynamic *this* binding ever allowed into the language?
09:26 25 Nov 2025

In JavaScript, the object that this refers to in an object method changes depending where the method is being called from:

window.myString = "what";
const o = {
    myString: "Hello world",
    logString(){
        console.log(this.myString);
    }
};

o.logString(); // logs "Hello world" to the console
const func = o.logString;
func(); // logs "what" to the console

In my many years of using JavaScript, I have never needed to dynamically change the meaning of this. In fact, it has been the cause of several mysterious bugs for me.

Why was this behavior ever allowed into the language?

javascript this