Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 901
vddmanikanta Avatar asked Apr 25 '26 20:04

vddmanikanta


1 Answers

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');
};
like image 158
Alexander Popov Avatar answered Apr 28 '26 08:04

Alexander Popov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!