Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript inheritance and method overriding

Assume I have a class like this:

function Widget() {
    this.id = new Date().getTime();
    // other fields
}
Widget.prototype = {
    load: function(args) {
        // do something
    }
}

From this class I created some other classes which inherit the same prototype but have some added methods. What I want to do is being able to define a load() method in the sub-classes which first calls the parent method and then execute some code. Something like:

SpecialWidget.prototype = {
    load: function(args) {
        super.load(args);
        // specific code here
    }
}

I know there's no super keyword in Javascript but there must be a way to do this.

like image 629
The Coding Monk Avatar asked Jan 24 '11 00:01

The Coding Monk


People also ask

Does JavaScript support method overriding?

It is true that JavaScript supports overriding, not overloading. When you define multiple functions that have the same name, the last one defined will override all the previously defined ones and every time when you invoke a function, the last defined one will get executed.

What is the difference between method overriding and inheritance?

Inheritance enable us to define a class that takes all the functionality from parent class and allows us to add more. Method overriding occurs simply defining in the child class a method with the same name of a method in the parent class .

Can we override a method in inheritance?

Instance MethodsThe ability of a subclass to override a method allows a class to inherit from a superclass whose behavior is "close enough" and then to modify behavior as needed. The overriding method has the same name, number and type of parameters, and return type as the method that it overrides.

Does JavaScript support overriding or overloading concept?

JavaScript does not support overloading. JavaScript supports overriding, so if you define two functions with the same name, the last one defined will override the previously defined version and every time a call will be made to the function, the last defined one will get executed.


3 Answers

You can simulate it like this:

SpecialWidget.prototype = {     load: function(args) {         Widget.prototype.load.call(this, args);         // specific code here     } } 

Or you can create your own super property like this:

SpecialWidget.prototype.parent = Widget.prototype;  SpecialWidget.prototype = {     load: function(args) {         this.parent.load.call(this,args);         // specific code here     } } 
like image 155
karim79 Avatar answered Sep 30 '22 01:09

karim79


so first, you set up your 'subclass' like so

function SubClass(name) {     Super.call(this);      // stuff here }  SubClass.prototype = new SuperClass(null); SubClass.prototype.constructor = SubClass; 

and then you can do

SuperClass.prototype.theMethod.apply(this); 

from within a subclass implementation to specifically invoke the super's implementation.

like image 32
hvgotcodes Avatar answered Sep 30 '22 02:09

hvgotcodes


I don't know if this is the best solution, but you could do something like this:

function Widget() {
    this.id = new Date().getTime();
}
Widget.prototype.load = function(args) {
   alert( 'parent load' );
};

SpecialWidget = function(){};

   // Make the prototype of SpecialWidget an instance of Widget
var proto = SpecialWidget.prototype = new Widget;

   // Give the prototype a function that references the "load" from Widget
proto.parent_load = proto.load;

   // Give SpecialWidget its own "load" that first calls the parent_load
proto.load = function( args ) {
    this.parent_load( args );
    alert( 'special load' );
};

var inst = new SpecialWidget;

inst.load();

This makes the prototype of SpecialWidget an instance of Widget so that it inherits all that Widget has.

Then it makes a reference to the load() of Widget called parent_load(), and creates its own load() that calls the parent_load() when invoked.

like image 36
user113716 Avatar answered Sep 30 '22 01:09

user113716