The term “this” (referred to as “the context”) holds a unique significance within every function, its value exclusively determined by the manner in which the function is invoked, independent of its definition’s context, timing, or location. Unlike other variables, it remains unaffected by lexical scopes, barring arrow functions, as elaborated below. Below are illustrative examples:
function foo() {
console.log(this);
}
// normal function call
foo(); // `this` will refer to `window`
// as object method
var obj = {bar: foo};
obj.bar(); // `this` will refer to `obj`
// as constructor function
new foo(); // `this` will refer to an object that inherits from `foo.prototype`