Given the following:
function MyClass() {
this.name = "Agnus"
return function() {
console.log(this.name)
}
}
const user = new MyClass() //this = "MyClass"
const r = MyClass() //this = "window"
console.log(name) //Agnus, this = "window"
r() //Agnus, this = "window"
My expectation was that on calling r(), I will get a error as the value of "this" is window for its case but instead it returned me "Agnus". On checking, I found that the properties of the object constructor MyClass are available from global.
I am having little difficulty to wrap my mind around such behavior, so it will be really nice if someone can explain me a bit about this.
Thank you for your help.