Learning About Prototypes and Inheritance with JavaScript
I never realized you could do object-like class inheritance with JavaScript until I worked with a JavaScript library called OpenLayers. The OpenLayers library allows you to create some rather amazing maps with Geographic Information Systems (GIS) data.
One of the major strengths of this library is that you can extend "classes" to make your own map-based controls. Although I was a bit skeptical about being able to do inheritance with JavaScript, my doubts were removed after learning about JavaScript prototyping. Even if it's not "true" object inheritance, you can make your JavaScript behave like class inheritance in other languages such as Java or C# with little effort.
In this article, I share what I learned so you can see the benefits of prototype inheritance with JavaScript. To get started, I explain the concept of prototypes and how they relate to class objects in other languages.
JavaScript Prototypes
There used to be (and probably still is) a long-running argument on the web about whether JavaScript was a "true" object-oriented language. Some would argue it was; others would say the opposite. To get more of an idea of what JavaScript really is, and how it relates to object-oriented (OO) concepts, is to determine whether objects can inherit methods from classes from which they were derived.
JavaScript does not have the concept of "classes" per se, but it has what are referred to as "prototypes." In strong-typed languages such as Java and C#, you have class constructors. These constructors are a method of that object in which they initialize the object instantiation.
In these languages, if no constructor is defined for the object, an implicit one is provided by that language's compiler. These constructors are referred to as "implicit" constructors.
In JavaScript, everything is an object. Therefore, JavaScript can determine what an object type is at runtime. You can refer to it as "dynamic" typing. Both Java and C# now have mechanisms to perform "dynamic" typing. Does this mean that everything is an object in those languages, too? Not necessarily; the compiler for those languages does some sort of "dependency injection" to define types while the programming is running.
Because everything in JavaScript is truly an object, so is a function's constructor. For example, take the following JavaScript code:
function Test(){ testVar = true;} var t = new Test();
In the above example, Test is now the constructor for the variable t. Because Test is an object, it can have properties such as Test.prototype. Internally, JavaScript is making a cloned object of Test and assigning this prototype to the variable t. Now t will inherit all the properties of the Test object.
For each cloned object, Java creates a prototype property that is used by the inheriting object. The prototype property basically serves as the liaison between the base (parent) object and the inheriting or "child" object.
So how does object B get object A's properties? It gets them through the cloned prototype property.
Here is an example:
function Test(){ } Test.prototype.nextVar = true; var t = new Test();
In this example, t now inherits all the Test properties through the cloned object's prototype property.
So, now you can do this:
t.nextVar = false;
Therefore, t now sets its inherited Boolean variable to false.
Note the following example. The newVar variable belongs only to the object t. In this context, t inherited the Test object properties, but also had one of its own. You might recognize this concept from other languages as an abstract type. Test is now an abstract type from which t inherits.
function Test(){ } Test.prototype.nextVar = true; var t = new Test(); t.newVar = false;
What if Test has a var that wasn't assigned through the prototype property? Would t inherit this property, too? The answer is no because only the prototype property of the cloned object can define the objects to which the t object gets access.
So, now the main question is this: can JavaScript do true "OO"-type inheritance? In a sense, yes, but only by cloning the base (parent) object. This actually creates two objects in memory as opposed to one object instance as with other OO languages.
In effect, JavaScript is replicating objects and using the cloned object's prototyped properties thru which objects to be inherited are defined. Technically, then, JavaScript is not an OO language such as C# or Java because base objects are cloned.
Also, as with other strongly typed languages, you can override the base class methods in the form of virtual abstract typing. With JavaScript, you cannot override any native objects defined in the base object that were not defined through this object's prototype.
You could argue that interfaces in other languages are basically cloned property definitions of a base class, through which other objects inherit these properties. However, with abstract types, you are truly getting direct access to the base object instance from another object.
Python is a powerful language because it virtually combines prototyping with true OO concepts such as inheritance and polymorphism.
Python could be considered as getting the best of both worlds. I challenge you to start coding your JavaScript using the inheritance described in this article. It may lead to more reusable JavaScript code, which is always beneficial for keeping a smaller footprint and for performance advantages.
Conclusion
In this article, you learned how to use JavaScript to perform object inheritance. The important thing to remember is that JavaScript assigns all objects that the inheriting object will receive through the cloned base object's prototype property.
In this context, remember that when doing inheritance with JavaScript, make sure to define all properties using the prototype property of what will be the cloned object that the inheriting object will use.
Although the examples in this article were simple and meant to define conceptually how to perform inheritance with JavaScript, it can become more complicated with "prototype chaining." You can probably imagine how confusing this might become. In essence, it achieves multiple inheritance as with other languages, but by using prototypes.
Based on what you learned, can JavaScript perform OO functions such as encapsulation? I do not know that you truly can do this because encapsulation prevents direct access to objects, which is what JavaScript is all about.
Maybe creating proxy objects in JavaScript could be one way to achieve this—although it would have to be researched and might become the topic of my next article :-).