The trouble with new
We have a simple JavaScript class:
We create a new User:
All is well. Unless we forgot the 'new':
Curses! That's not what we want at all. By omitting the 'new' keyword, the JavaScript engine executes our 'User' constructor in the current scope, which in this case is the global window object. With the scope ('this') set to window, setting 'this.name' is now the same as setting 'window.name', which is not what we're trying to do.