Just JavaScript - Object Construction |
Written by Ian Elliot | |||||||
Thursday, 21 August 2014 | |||||||
Page 3 of 3
Constructor pitfallsThis 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. For example:
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. For example:
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?The issue of using an object factory or an object constructor is a difficult topic. Most JavaScript programmers are introduced to using constructors as if it is the standard way of creating objects - it is. However it has problems. The first is that you have to remember to use new in front of a constructor call - and if you forget it doesn't work. You don't need to use new in front of an object factory call and the use of self produces early bound methods and properties. The problem is that using a constructor brings will it some additional actions that make programming easier that will be the subject of the next chapter - in particular the constructor property and the prototype property are automatically set when you use new and a constructor 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. For example:
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.
Summary
Just JavaScriptThere is a newer version of the draft of the book here. A Radical Look At JavaScriptContents
-Preface-Most books on JavaScript either compare it to the better known class based languages such as Java or C++ and even go on to show you how to make it look like the one of these. Just JavaScript is an experiment in telling JavaScript's story "just as it is" without trying to apologise for its lack of class or some other feature. The broad features of the story are very clear but some of the small details may need working out along the way - hence the use of the term "experiment". Read on, but don't assume that you are just reading an account of Java, C++ or C# translated to JavaScript - you need to think about things in a new way. Just JavaScript is a radical look at the language without apologies. Coming NextThe inheritance problem - prototype and type. Related ArticlesJavascript Jems - Object factories, constructors and clones Javascript Jems - a new take on objects
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.
Comments
or email your comment to: comments@i-programmer.info
<ASIN:0596805527> <ASIN:193398869X> <ASIN:0137054890> <ASIN:1449381871> <ASIN:1430230541> |
|||||||
Last Updated ( Sunday, 10 May 2015 ) |