Method containing “this” keyword works different when it's called in another method vs when it's assigned...





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







1
















This question already has an answer here:




  • How does the “this” keyword work?

    23 answers




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?










share|improve this question















marked as duplicate by Bergi javascript
Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 24 '18 at 17:53


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • 1





    It doesn't matter where it is called, it matters how it is called - obj.Play() vs. Play(). You also should use strict mode.

    – Bergi
    Nov 24 '18 at 17:53


















1
















This question already has an answer here:




  • How does the “this” keyword work?

    23 answers




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?










share|improve this question















marked as duplicate by Bergi javascript
Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 24 '18 at 17:53


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • 1





    It doesn't matter where it is called, it matters how it is called - obj.Play() vs. Play(). You also should use strict mode.

    – Bergi
    Nov 24 '18 at 17:53














1












1








1









This question already has an answer here:




  • How does the “this” keyword work?

    23 answers




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?










share|improve this question

















This question already has an answer here:




  • How does the “this” keyword work?

    23 answers




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?





This question already has an answer here:




  • How does the “this” keyword work?

    23 answers







var game = "Football";

function Play() {
console.log(this.game)
}

function PlayGround() {
this.Play = Play;
}

var obj = new PlayGround();
obj.Play();





var game = "Football";

function Play() {
console.log(this.game)
}

function PlayGround() {
this.Play = Play;
}

var obj = new PlayGround();
obj.Play();





var game = "Football";

function Play() {
console.log(this.game)
}

function PlayGround() {
Play();
}

var obj = new PlayGround();





var game = "Football";

function Play() {
console.log(this.game)
}

function PlayGround() {
Play();
}

var obj = new PlayGround();






javascript constructor this ecmascript-5






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 24 '18 at 17:20









Patrick Roberts

21.3k33777




21.3k33777










asked Nov 24 '18 at 17:19









george sacageorge saca

205




205




marked as duplicate by Bergi javascript
Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 24 '18 at 17:53


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









marked as duplicate by Bergi javascript
Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 24 '18 at 17:53


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.










  • 1





    It doesn't matter where it is called, it matters how it is called - obj.Play() vs. Play(). You also should use strict mode.

    – Bergi
    Nov 24 '18 at 17:53














  • 1





    It doesn't matter where it is called, it matters how it is called - obj.Play() vs. Play(). You also should use strict mode.

    – Bergi
    Nov 24 '18 at 17:53








1




1





It doesn't matter where it is called, it matters how it is called - obj.Play() vs. Play(). You also should use strict mode.

– Bergi
Nov 24 '18 at 17:53





It doesn't matter where it is called, it matters how it is called - obj.Play() vs. Play(). You also should use strict mode.

– Bergi
Nov 24 '18 at 17:53












1 Answer
1






active

oldest

votes


















3














The first one outputs undefined very simply because obj does not have a game property. That can be fixed like this:






var game = "Football";

function Play() {
console.log(this.game)
}

function PlayGround() {
this.game = game;
this.Play = Play;
}

var obj = new PlayGround();
obj.Play();





The second snippet is bad practice because it relies on mechanics of non-strict mode simple calls in order to work.



If you change the second snippet to strict mode it will error because this in simple calls is undefined instead of window:






"use strict";

var game = "Football";

function Play() {
console.log(this.game)
}

function PlayGround() {
Play();
}

var obj = new PlayGround();





If you use let or const declaration for game, the second snippet will output undefined because those top-level declarations do not attach to the global scope window:






let game = "Football";

function Play() {
console.log(this.game)
}

function PlayGround() {
Play();
}

var obj = new PlayGround();








share|improve this answer






























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    3














    The first one outputs undefined very simply because obj does not have a game property. That can be fixed like this:






    var game = "Football";

    function Play() {
    console.log(this.game)
    }

    function PlayGround() {
    this.game = game;
    this.Play = Play;
    }

    var obj = new PlayGround();
    obj.Play();





    The second snippet is bad practice because it relies on mechanics of non-strict mode simple calls in order to work.



    If you change the second snippet to strict mode it will error because this in simple calls is undefined instead of window:






    "use strict";

    var game = "Football";

    function Play() {
    console.log(this.game)
    }

    function PlayGround() {
    Play();
    }

    var obj = new PlayGround();





    If you use let or const declaration for game, the second snippet will output undefined because those top-level declarations do not attach to the global scope window:






    let game = "Football";

    function Play() {
    console.log(this.game)
    }

    function PlayGround() {
    Play();
    }

    var obj = new PlayGround();








    share|improve this answer




























      3














      The first one outputs undefined very simply because obj does not have a game property. That can be fixed like this:






      var game = "Football";

      function Play() {
      console.log(this.game)
      }

      function PlayGround() {
      this.game = game;
      this.Play = Play;
      }

      var obj = new PlayGround();
      obj.Play();





      The second snippet is bad practice because it relies on mechanics of non-strict mode simple calls in order to work.



      If you change the second snippet to strict mode it will error because this in simple calls is undefined instead of window:






      "use strict";

      var game = "Football";

      function Play() {
      console.log(this.game)
      }

      function PlayGround() {
      Play();
      }

      var obj = new PlayGround();





      If you use let or const declaration for game, the second snippet will output undefined because those top-level declarations do not attach to the global scope window:






      let game = "Football";

      function Play() {
      console.log(this.game)
      }

      function PlayGround() {
      Play();
      }

      var obj = new PlayGround();








      share|improve this answer


























        3












        3








        3







        The first one outputs undefined very simply because obj does not have a game property. That can be fixed like this:






        var game = "Football";

        function Play() {
        console.log(this.game)
        }

        function PlayGround() {
        this.game = game;
        this.Play = Play;
        }

        var obj = new PlayGround();
        obj.Play();





        The second snippet is bad practice because it relies on mechanics of non-strict mode simple calls in order to work.



        If you change the second snippet to strict mode it will error because this in simple calls is undefined instead of window:






        "use strict";

        var game = "Football";

        function Play() {
        console.log(this.game)
        }

        function PlayGround() {
        Play();
        }

        var obj = new PlayGround();





        If you use let or const declaration for game, the second snippet will output undefined because those top-level declarations do not attach to the global scope window:






        let game = "Football";

        function Play() {
        console.log(this.game)
        }

        function PlayGround() {
        Play();
        }

        var obj = new PlayGround();








        share|improve this answer













        The first one outputs undefined very simply because obj does not have a game property. That can be fixed like this:






        var game = "Football";

        function Play() {
        console.log(this.game)
        }

        function PlayGround() {
        this.game = game;
        this.Play = Play;
        }

        var obj = new PlayGround();
        obj.Play();





        The second snippet is bad practice because it relies on mechanics of non-strict mode simple calls in order to work.



        If you change the second snippet to strict mode it will error because this in simple calls is undefined instead of window:






        "use strict";

        var game = "Football";

        function Play() {
        console.log(this.game)
        }

        function PlayGround() {
        Play();
        }

        var obj = new PlayGround();





        If you use let or const declaration for game, the second snippet will output undefined because those top-level declarations do not attach to the global scope window:






        let game = "Football";

        function Play() {
        console.log(this.game)
        }

        function PlayGround() {
        Play();
        }

        var obj = new PlayGround();








        var game = "Football";

        function Play() {
        console.log(this.game)
        }

        function PlayGround() {
        this.game = game;
        this.Play = Play;
        }

        var obj = new PlayGround();
        obj.Play();





        var game = "Football";

        function Play() {
        console.log(this.game)
        }

        function PlayGround() {
        this.game = game;
        this.Play = Play;
        }

        var obj = new PlayGround();
        obj.Play();





        "use strict";

        var game = "Football";

        function Play() {
        console.log(this.game)
        }

        function PlayGround() {
        Play();
        }

        var obj = new PlayGround();





        "use strict";

        var game = "Football";

        function Play() {
        console.log(this.game)
        }

        function PlayGround() {
        Play();
        }

        var obj = new PlayGround();





        let game = "Football";

        function Play() {
        console.log(this.game)
        }

        function PlayGround() {
        Play();
        }

        var obj = new PlayGround();





        let game = "Football";

        function Play() {
        console.log(this.game)
        }

        function PlayGround() {
        Play();
        }

        var obj = new PlayGround();






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 24 '18 at 17:31









        Patrick RobertsPatrick Roberts

        21.3k33777




        21.3k33777

















            這個網誌中的熱門文章

            Xamarin.form Move up view when keyboard appear

            Post-Redirect-Get with Spring WebFlux and Thymeleaf

            Anylogic : not able to use stopDelay()