Javascript Jems - Type and the constructor |
Written by Ian Elliot | |||||||
Thursday, 23 September 2010 | |||||||
Page 3 of 3
Testing which function created an object can be useful if you are trying to build a mechanism for restricting what people can do with objects you provide them with. It allows you to implement some of the features of a type system by regarding type as being determined by the constructor. For example in the previous article the idea of using instanceof to test it this was correctly set was described: function Point(){ If Point is used with new this will be set to reference the anonymous object that is being constructed and the anonymous object's constructor property will already have been automatically set to reference the Point function. Notice that this is not a reference to Point. A more transparent, but not identical, way of writing this function is: function Point(){ Finally notice that there is also a typeof operator but it returns "object" for every custom object and its main role in life is to let you identify primitive datatypes.and built-in objects. Object factories as constructorsYou may at this point be wondering if object factories can be regarded as constructors? The answer is yes.. and no. There is nothing stopping you from setting the constructor property of any given object to anything you like. This might be seen as a disadvantage of the system because it looks as if you can fool the Javascript type system in this way - as it turns out you can't, but more on this later. So to assign an object factory as the constructor of an object you would use something like: function NewPoint(){ Notice the assignment to the constructor property. Now if you try: var point= NewPoint(); you will discover that NewPoint is indeed the constructor of point and the test evaluates to true. However if you now try: alert(point instanceof NewPoint); you will, probably, be surprised to see false displayed. The reason is that instanceof actually uses a more complicated way of determining if the given function was used in creating an object than it just being named as the constructor. To be more precise it makes use of the objects prototype chain but exactly how this works takes us into Javascript's form of inheritance. Finally notice that if you have an object you can use the constructor property to get another one. For example: var point= NewPoint(); Naming constructorsSo the bottom line is that Javascript regards and object's type and its constructor function as one and the same. What this means is that if you are going to test for an objects "type" using instanceof you have to use sensible names for constructors and you have to use constructors rather than object factories. In particular you don't want to call a constructor something like: function CreatePoint(){... because then you have to write if( mypoint instanceof CreatePoint )... So much better to use a constructor called Point and then be able to write if( mypoint instanceof Point)... Notice that we are slowly but surely starting to regard the constructor function object as a surrogate class for the object it creates. This is a shame and misses the elegance of the way that Javascript does object. The next topic we have to deal with is prototype inheritance and this more or less concludes our look at the way Javascript does objects. If you would like to be informed about new articles on I Programmer you can either follow us on Twitter, on Facebook , on Digg or you can subscribe to our weekly newsletter. <ASIN:0137054890> <ASIN:0321572602> <ASIN:0470684143>
|
|||||||
Last Updated ( Monday, 04 October 2010 ) |