Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a good pattern for JavaScript calling overridden functions in a parent without knowing the specific parent?

Tags:

javascript

Basically I want inheritable functions as in

Base = function() { };

Base.prototype.foo = function() {
  console.log("base foo");
};

Derived = function() { };

somelib.inherit(Derived, Base);

Derived.prototype.foo = function() {
  console.log("derived foo");
}

d = new Derived():
d.foo();

And I want it to print

derived foo
base foo

Yes I know I can explicitly call Base.prototype.foo.call(this); I'm just wondering if there is a pattern for calling overridden super class functions automatically. The problem I'm trying to solve is 2 fold.

  1. derived classes should NOT have to remember to call their parent's method, it just happens automatically.
  2. if 1. can't happen then at least I'd like Derived not to call Base by name since that's brittle. Rather I'd like it call parentclass or something so you don't have to know the base. That way if you change the name of the base you don't have to go fixing every derived class.
like image 863
gman Avatar asked Aug 25 '11 11:08

gman


People also ask

Which of the following keywords is used to override the parent class methods in JavaScript?

The super keyword is used to call the constructor of its parent class to access the parent's properties and methods. Tip: To understand the "inheritance" concept (parent and child classes) better, read our JavaScript Classes Tutorial.

Is method overriding possible in JavaScript?

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.

How do I stop JavaScript overriding?

You can use Object. freeze(obj) to set the entire returned object to be immutable. Additionally, note that you can use const instead of var to avoid the object being reassigned.

What is method overriding in JavaScript?

Method Overriding is an OOPs concept closely knit with inheritance. When a child class method overrides the parent class method of the same name, parameters and return type, it is termed as method overriding.


1 Answers

You can implement such functionality by using a structure like:

function Base(){}
Base.prototype.destroy = function(){console.log('Base destroy');};

function Derived(){}
Derived.prototype = new Base; // Let Derived inherit from Base

// Override the `destroy` method
Derived.prototype.destroy = function() {
    console.log('Derived destroy');

    // Call parent class method
    this.constructor.prototype.destroy();
    // If the context of the method is important, you can use Function.call:
  //this.constructor.prototype.destroy.call(this);
};


// Create an instance of Derived, and call the destroy method:
(new Derived).destroy();
like image 80
Rob W Avatar answered Oct 12 '22 21:10

Rob W