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' }
{ 'key1' : 'val1' , 'key2' : { 'inner_key1' : 'inner_val1' } , 'key3' : function() { return 10 + 5; } }
var myObject = { 'key1' : 'val1' , 'key2' : 'val2' , 'key3' : 'val3' }
var myObject = { 'key1' : 'val1' , 'key2' : { 'inner_key1' : 'inner_val1' } , 'key3' : function() { return 10 + 5; } }
myObject.key1; myObject.key2.inner_key1; myObject.key3();
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); } }
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"
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();
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"
var SubClass = function() { this.subFunction = function() { return 'function in Sub Class'; } } SubClass.prototype = new SuperClass();
var sub = new SubClass(); sub.superFunction(); // will return the string "function in Super Class" sub.subFunction(); // will return the string "function in Sub Class"
SuperClass.prototype.myFunc = function() { return 'my new function'; }
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"
var sup = new SuperClass(); sup.myFunc(); // will return the string "function in Super Class"
No comments:
Post a Comment