Javascript Jems - Prototype Inheritance |
Written by Ian Elliot | |||||||||||||
Monday, 18 October 2010 | |||||||||||||
Page 3 of 3
Prototype chainsThis story has been long, hard and detailed and its why its difficult for beginners to get it right. Now we have one last but fairly obvious extension. When you specify an object as being the prototype object that object can too take part in the prototype mechanism. That is you can write a constructor for object A. You can then write a constructor for object B which has its prototype property set to an instance of object A. You can then write a constructor for object C which has its prototype property set to an instance of object B and so on. Now if you try to access a property or method on object C Javascript first looks to see if it is defined within object C. If it isn't it then looks at the prototype to look to see if it is defined in the instance of object B. If it isn't then it looks at object B's prototype i.e. object A to see if it is defined there. You can see that you can build up a prototype chain to implement multiple steps of inheritance. Object C inherits from object B which inherits from object A. Javascript automatically searches the prototype chain to find any undefined properties or methods and uses the first example it finds. For example, suppose we define Point1D as: function Point1D() and Point2D as: function Point2D() and finally Point3D as: function Point3D() Now if we create an instance of Point3D: var point=new Point3D(); then the new object has a single property of its own - that is z but if you reference a y property the property chain is used and it is found in Point2D. If you reference a property x then this isn't found in Point2D but in Point1D. That is a Point3D object inherits x and y from Point1D and Point 2D respectively. As Point3D inherits from Point2D and Point1D Javascript also searches the prototype chain when testing type using instanceof. That is: point instanceof Point3D is true and so are: point instanceof Point2D and point instanceof Point1D This is Javascript's equivalent of a type hierarchy. SummaryIt has been a long story but now you are in a position to see everything from a great height.
The most important thing to realise is that Javascript can be used in a very simple object oriented way that doesn't use inheritance at all. Dynamic objects can be created, modified and used without any need of the old class oriented ideas of inheritance and type. The only time when there is a strong compulsion to use the prototype mechanims is when there are lots of objects involved and you need to implement methods more efficiently. That is methods provided by the constructor aren't shared between instances but methods provided by the prototype chain are shared - hence reducing the size of the code. Javascript has a very good approach to objects and one that suits a dynamic language. However being such a dynamic language there are more ways of doing the same job than you might expect. Put simply there are other ways of making Javascript objects make use of properties and methods implemented by other objects, i.e. other forms of inheritance. To find out more look out for the next article. 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:0321683919> <ASIN:0596806752> |
|||||||||||||
Last Updated ( Monday, 18 October 2010 ) |