acbrent25
9/4/2017 - 3:59 PM

How Timeout's function with this

How Timeout's function with this

<!-- Context.html -->
<!-- How Timeout's function with this -->

<!DOCTYPE html>

<html lang="en-us">

  <head>

    <meta charset="UTF-8">

    <title>Context</title>

  </head>

  <body>

    <!-- Script -->
    <script type="text/javascript">

      // We define a person, and include a method that logs his name along with a greeting.
      var person = {
        name: "Jeff",
        sayHi: function() {
          console.log("Hi, I'm " + this.name);
          console.log(this);
        }
      };

      // When we run it normally, it works like we intended.
      person.sayHi();

      // Hi, I'm Jeff
      // But if we include it in a timeout
      setTimeout(person.sayHi, 2000);
      
      // We get the following:
      // Hi, I'm
      // Window Object

    </script>

  </body>

</html>