Always this keyword refers to the context that it was called in. But self and window even after changing context refers to Window itself.
// Log function
function logAll(){
console.log("this >", this);
console.log("self >", self);
console.log("window >", window);
console.log("window.self >", window.self);
console.log("this.self >", this.self);
};
// Class Car
function Car(){
this.self = 6;
};
// Instance of Car
var honda = new Car();
honda.self = 5;
// logAll() called in context Gloabal
logAll();
// Outputs
this > Window
self > Window
window > Window
window.self > Window
this.self > Window
// logAll() called in context honda
logAll.call(honda);
// Outputs
this > Car { self= 5 }
self > Window
window > Window
window.self > Window
this.self > 5
Here you can see this.self returns window in when called in global scope because in global scope
this = windows
this.self = windows.self
this.self = Windows
But while you are in the context of honda, an instance of Car:
this = honda
this.self = honda.self
this.self = 5
Context plays an important role in Javascript, while working with OOPs and inheritance.