Tuesday 30 May 2017

What is "this" in immediately-invoking functions?

I guess this in immediately-invoking functions always refers to the global/window object in javascript, i.e. the result should be true in both nodejs and browser shown below. But this is not the case, I got a false in nodejs, why? The code is completely the same.

main.js

var global = this;
(function () {
    console.log(global === this); // false
})();

index.html

<html>
  <head></head>
  <body>
    <script>
      var global = this;
      (function () {
          console.log(global === this); // true
      })();
    </script>
  </body>
</html>



via Searene

No comments:

Post a Comment