What does the following code do:
WeatherWidget.prototype = new Widget;
where Widget
is a constructor, and I want to extend the
According to some odd Javascript rules, new Widget
actually invokes the constructor rather than returning a reference to the constructor. This question actually answers the question the difference between var a = new Widget()
and var a = Widget()
.
In simple words, the new
keyword tells Javascript to call the function Widget
under a different set of rules than a regular function call. Going off the top of my head, the ones I remember are:
Widget
can use the this
keyword to refer to that object.Widget
does not return anything, this new object will be created.Widget
that are used to track down property chains.Without the new
keyword, a call to widget would
this
will be set to undefined.
this
will refer to the global object. (Called window
by the browser.)undefined
will be returned.Reference: new keyword