Having the following code, the output is undefined when calling the Play method from my object instance while I was expecting the 'this' keyword to reference the global variable and display "Football" in lack of a PlayGound's scope variable.
var game = "Football";
function Play() {
console.log(this.game)
}
function PlayGround() {
this.Play = Play;
}
var obj = new PlayGround();
obj.Play();
Now, invoking the Play() at constructor level, without passing it's reference to a local variable the output is "Football".
var game = "Football";
function Play() {
console.log(this.game)
}
function PlayGround() {
Play();
}
var obj = new PlayGround();
Can someone explain the reason why those two aproaches behave differently?