This is because, before ECMAscript 262 edition 5, there was a lot of confusion if people using the constructor pattern forgot to use the new keyword. If you forgot to use new when invoking the constructor function in ES3, this referred to the global object ( window in the browser), and you would have attached the global object to variables.
This was terrible behavior, and so people at ECMA decided to set this to undefined .
Example:
function myConstructor() { this.a = 'foo'; this.b = 'bar'; } myInstance = new myConstructor();
The last line will output an error in ES5 strict
"TypeError: this is undefined"
(which is much better)
jAndy Mar 22 '12 at 12:50 2012-03-22 12:50
source share