Thursday, August 4, 2011

Object Oriented JavaScript: Inheritance

Object Oriented JavaScript: Inheritance

Objects and JavaScript

JavaScript, being a functional language, differs from most of the procedural/object oriented languages we know. The object oriented approach in JavaScript is rather strange. However there is much power in making objects! The syntax is really odd and there are several approaches.

Literal Notation

As many of you may know the most used notation is the JSON (JavaScript Object Notation).
{ 'key1' : 'val1'
, 'key2' : 'val2'
, 'key3' : 'val3'
}
Of course this is the very basic example. You can use as value any JavaScript object – another similar object or a function.
{ 'key1' : 'val1'
, 'key2' : { 'inner_key1' : 'inner_val1' }
, 'key3' : function() {
   return 10 + 5;
   }
}
The two examples above are showing an anonymous object in JavaScript, but you can assign this code to some variable.
var myObject = 
 { 'key1' : 'val1'
 , 'key2' : 'val2'
 , 'key3' : 'val3'
 }
or
var myObject =
 { 'key1' : 'val1'
 , 'key2' : { 'inner_key1' : 'inner_val1' }
 , 'key3' : function() {
    return 10 + 5;
    }
 }
and then you can call the properties of these objects with the ‘.’ operator:
myObject.key1;
myObject.key2.inner_key1;
myObject.key3();
So far so good – this is the literal object notation in JavaScript. However there is another “objects” in JavaScript.

Objects and Functions in JavaScript

Actually in JS the functions are also objects (classes). So you can define a function and then define another function inside or use the “this” operator.
var a = function(name)
{
 this.name = name;
 
 this.print = function() {
  alert(this.name); 
 }
}
Then you can make a new instance of this “class”.
var myObject = new a('hello world');
var anotherObject = new a('some test');
 
myObject.print(); // will print the string "hello world"
anotherObject.print(); // will print the string "some test"
Thus the function “a” defined within the code “var a = function(name) …” is a class in JavaScript. You can make instances (objects) of this class with the “new” operator.
You can use another notation for defining the function “a”, so both can be used and it’s up to you to decide which one is more convenient.
function a(name) 
{
 this.name = name;
 
 this.print = function() {
  alert(this.name); 
 } 
}

Prototypes

JS gives you more power. In most of the languages you’ve to define the class first and then make objects. In JS you can define a “class” then make some objects and then add some properties to the class by using the prototype.
var a = function(name) 
{
 this.name = name;
}
 
var myObj = new a('hello world');
 
a.prototype.print = function() {
 alert(this.name); 
}
 
myObj.print();
So you can see how you can add functions and properties to an already defined class using the prototype. Thus you can call the “print” function on the myObj object.

Inheritance

The prototype built-in property can be used to make use of one of the main OOP features – inheritance.
var SuperClass = function()
{
 this.superFunction = function() {
  return 'function in Super Class'; 
 }
}
 
var sup = new SuperClass();
sup.superFunction(); // will return the string "function in Super Class"
Now you can add one more class and set its prototype to the SuperClass
var SubClass = function()
{
 this.subFunction = function() {
  return 'function in Sub Class'; 
 } 
}
 
SubClass.prototype = new SuperClass();
Thus you make SubClass inherit from SuperClass. Now you can call the SuperClass properties from any SubClass object.
var sub = new SubClass();
sub.superFunction(); // will return the string "function in Super Class"
sub.subFunction(); // will return the string "function in Sub Class"
Even more! You can add properties to the super class with prototype.
SuperClass.prototype.myFunc = function() {
 return 'my new function'; 
}
And then call it from the sub class:
sub.myFunc(); // will return the string "my new function"

Override a Function

What happens if you have the same function name in both classes. Actually this overrides the method in the sub class.
var SuperClass = function()
{
 this.myFunc = function() {
  return 'function in Super Class'; 
 }
}
 
var SubClass = function()
{
 this.myFunc = function() {
  return 'function in Sub Class'; 
 } 
}
 
SubClass.prototype = new SuperClass();
 
var sub = new SubClass();
sub.myFunc(); // will return the string "function in Sub Class"
while
var sup = new SuperClass();
sup.myFunc(); // will return the string "function in Super Class"

Conclusion

Now you know there are classes, inheritance and overrides in JavaScript – thus you can improve your object oriented JavaScript!

No comments:

Post a Comment