0

In the following snippet, while console.log is same for the same code, why is it providing 2 different results while calling as function, making new object? I know that functions have their own scope in JS, so at-least console.log inside a function should have rendered same result for both cases. Why, the 2 results are different?

Also, how the hell is scope inside a function is taking window scope rather than its own (functions have their own scope, should this should not be same as window object).

<html>

<head></head>

<body>

  <script>
    e = 50;

    function alpha() {
      this.a = 10;
      this.b = 20;
      this.e;
      console.log(this.e);
    }

    alpha();
    var b = new alpha;
  </script>
</body>

</html>

Note: The duplicate question is general questioning not related to this code. I am asking for this particular problem, and is not explained in duplicate link. Pls, reopen.

Deadpool
  • 6,416
  • 5
  • 32
  • 64

0 Answers0