|Written by Ian Elliot
|Thursday, 21 August 2014
Page 3 of 3
This double use of this in a constructor is often confusing to beginners.
It also has the potential to cause some errors.
For example if you use a constructor without new then this is set to the current call context and not to a new empty object. The result is that the properties and methods that should have been added to the new object are added to the current call context. In most cases the call context is the global object so all of the properties and methods are added to the global object.
works because the constructor called without new adds x, y and add to the global object.
The hijacking of the this call context in the constructor also causes problems when new is used and for some reason the constructor wants to use the call context. However this very rarely happens because constructors are not usually methods of other objects.
There is nothing wrong with defining the Point constructor to be a property of a Geometry object, but because of the use of new this is set to reference the new object that is being created and not the Geometry object i.e. the call context. What this means is that that you cannot use this to reference the units property of the Geometry object. You can make it work but only by putting the Geometry object inside a constructor and giving it a private variable that references it so that the Point constructor can access it.
This is not a practical issue but it is important that the way that this is used in a constructor is completely understood. As soon as you put new in front of a function call this is no longer the call context.
Self or New?
There is a compromise you can use a constructor but use self rather than this by simply setting self to reference the same object as this.
Notice that self is set to this as the first instruction. The only real difference is that in the definition of the add function the properties are early bound to the instance. If you want to use this in the creation of the object and self in method definitions you can. Indeed you can choose to use self or this in method definition depending on whether you want them early or late bound to the call context.
You can even test for the use of new in a call to the constructor by testing if this is the global object, usually window. If this is the global object the constructor has been called without using new and you simple call the constructor properly:
this assumes that the global object is window. If you can't assume this set up a global variable to store a reference to the global object:
There are many variations in this basic theme. In general, however, the us of new in front of a constructor isn't a big problem.
There is a newer version of the draft of the book here.
The inheritance problem - prototype and type.
To be informed about new articles on I Programmer, install the I Programmer Toolbar, subscribe to the RSS feed, follow us on, Twitter, Facebook, Google+ or Linkedin, or sign up for our weekly newsletter.
or email your comment to: email@example.com
|Last Updated ( Sunday, 10 May 2015 )