0

When we use the "new" keyword in an instance of a constructor function in ES5, we basically change the scope of the this variable to that block (by creating a new block). To my knowledge, ES5 is a function scoped language unlike ES6 which is block scoped. So, the "this" keyword should still point to the global scope, right? So, this shouldn't work????

revmatcher
  • 757
  • 8
  • 17
  • The `new` keyword doesn't change between ES5 and ES6. And ES6 is still a function-scoped language just like ES5. Only the `let` keyword is block scoped – slebetman May 13 '20 at 04:17
  • Note that `this` is not involved in scope at all. The way `this` behaves is a different concept called binding - which class/prototype does `this` bind to – slebetman May 13 '20 at 04:19
  • If you want to know how `this` behaves in ES4, ES5, ES6, ES7 ++ read my answer to this question: https://stackoverflow.com/questions/13441307/how-does-the-this-keyword-in-javascript-act-within-an-object-literal/13441628#13441628. As long as I'm alive I'll keep updating it when javascript gets a new feature – slebetman May 13 '20 at 04:21
  • Thank you so much. Could you please explain the second point of your answer in https://stackoverflow.com/questions/13441307/how-does-the-this-keyword-in-javascript-act-within-an-object-literal/13441628#13441628 – revmatcher May 13 '20 at 06:04
  • It's the name before the last dot (`.`). For example in `x.y.z()` the `this` is bound to `y`. In `x.z()` the `this` is bound to `x`. In `a.c = a.b.c; a.c()` the `this` is bound to `a` even though the function `c` was originally defined as part of `b` – slebetman May 13 '20 at 06:08

0 Answers0