Object.extend, an example of inheritance
Updated: my previous attempt to simulate inheritance in javascript using Prototype.extend did not work as I expected so I removed the example I had written.
Inheritance can be defined in several ways. The definition of inheritance I will use in the following example is: A child class inherits from a parent class if all methods of parent are available to the child and the child methods override the parent's ones. The child methods which are not defined on the parent must of course be available on the child object.
[source:js]
function ParentObject() {}
ParentObject.prototype = {
commonMethod: function() { return "method defined on parent" };
methodToBeOverrridden: function() { return "you should never see me"}
};
ParentObject.extend = function(subclass) {
//we need a temp object to store our new prototype
function T(){};
//let's copy all parents methods
for (var p in this.prototype){
T.prototype[p] = this.prototype[p];
}
//let's copy all child methods
for (var p in subclass.prototype){
T.prototype[p] = subclass.prototype[p];
}
subclass.prototype = new T();
}
[/source]
And now let's define a simple class which will act as a child:
[source:js]
function DerivedObject() {}
DerivedObject.prototype = {
lessCommonMethod: function() { return "method defined on child" };
methodToBeOverrridden: function() { return "overridden!"};
}
[/source]
To use our subclass, we will run:
[source:js]
ParentObject.extend(DerivedObject);
var derivedObject = new DerivedObject();
derivedObject.lessCommonMethod(); // will print "method defined on child"
derivedObject.commonMethod(); // will print "method defined on parent"
derivedObject.methodToBeOverridden(); // will print "overridden!"
[/source]
Please observe that the parent constructor is totally ignored by this inheritance simulation
Inheritance can be defined in several ways. The definition of inheritance I will use in the following example is: A child class inherits from a parent class if all methods of parent are available to the child and the child methods override the parent's ones. The child methods which are not defined on the parent must of course be available on the child object.
[source:js]
function ParentObject() {}
ParentObject.prototype = {
commonMethod: function() { return "method defined on parent" };
methodToBeOverrridden: function() { return "you should never see me"}
};
ParentObject.extend = function(subclass) {
//we need a temp object to store our new prototype
function T(){};
//let's copy all parents methods
for (var p in this.prototype){
T.prototype[p] = this.prototype[p];
}
//let's copy all child methods
for (var p in subclass.prototype){
T.prototype[p] = subclass.prototype[p];
}
subclass.prototype = new T();
}
[/source]
And now let's define a simple class which will act as a child:
[source:js]
function DerivedObject() {}
DerivedObject.prototype = {
lessCommonMethod: function() { return "method defined on child" };
methodToBeOverrridden: function() { return "overridden!"};
}
[/source]
To use our subclass, we will run:
[source:js]
ParentObject.extend(DerivedObject);
var derivedObject = new DerivedObject();
derivedObject.lessCommonMethod(); // will print "method defined on child"
derivedObject.commonMethod(); // will print "method defined on parent"
derivedObject.methodToBeOverridden(); // will print "overridden!"
[/source]
Please observe that the parent constructor is totally ignored by this inheritance simulation
Comments
Post a Comment