vddmanikanta
vddmanikanta

Reputation: 71

Calling a super class method from the overridden method in child class in Java Script?

I have a function in base class.I have overridden that function in my child class.

Use-case: I want to set few properties inside my overridden method in child-class and then want to call the corresponding function in base class.

How can I achieve this JavaSScript?

Thank you With regards Deenadayal

Upvotes: 3

Views: 1749

Answers (1)

Alexander Popov
Alexander Popov

Reputation: 836

You can use call method. For instance:

function BaseClass(){}

BaseClass.prototype.someMethod = function()
{
    console.log('I\'m in the BaseClass');
};

function ChildClass()
{
    // call parent contructor, pass arguments if nedded
    BaseClass.call(this);
}

ChildClass.prototype = Object.create(BaseClass.prototype);
ChildClass.prototype.constructor = ChildClass;

// override method
ChildClass.prototype.someMethod = function()
{
    BaseClass.prototype.someMethod.call(this);
    console.log('I\'m in the ChildClass');
};

Upvotes: 4

Related Questions