extends in javascript

The extends keyword is used to create a child class of another class (parent). 
The child class inherits all the methods from another class. Inheritance is 
useful for code reusability: reuse properties and methods of an existing class 
when you create a new class.

4.33
3
Aleksandaril 130 points

                                    The extends keyword is used to create a child class of another class (parent). The child class inherits all the methods from another class. Inheritance is useful for code reusability: reuse properties and methods of an existing class when you create a new class.

4.33 (3 Votes)
0
3.5
2
Maven 110 points

                                    // parent class animal
class Animal {
    constructor(name, weight) {
        this.name = name;
        this.weight = weight;
    }

    eat() {
        return `${this.name} is eating!`;
    }

    sleep() {
        return `${this.name} is going to sleep!`;
    }

    wakeUp() {
        return `${this.name} is waking up!`;
    }

}
//sub class gorilla

class Gorilla extends Animal {
    constructor(name, weight) {
        super(name, weight);
    }

    climbTrees() {
        return `${this.name} is climbing trees!`;
    }

    poundChest() {
        return `${this.name} is pounding its chest!`;
    }

    showVigour() {
        return `${super.eat()} ${this.poundChest()}`;
    }

    dailyRoutine() {
        return `${super.wakeUp()} ${this.poundChest()} ${super.eat()} ${super.sleep()}`;
    }

}

function display(content) {
    console.log(content);
}

const gorilla = new Gorilla('George', '160Kg');
display(gorilla.poundChest());
display(gorilla.sleep());
display(gorilla.showVigour());
display(gorilla.dailyRoutine());

// OUTPUT:
// George is pounding its chest!
// George is going to sleep!
// George is eating! George is pounding its chest!
// George is waking up! George is pounding its chest! George is eating! G
/*
The above code has 2 JavaScript classes namely Animal and Gorilla.
The Gorilla class is a subclass or child class of Animal and it uses the extends keyword to set itself as a subclass.
However, the super keyword has been used in two different ways.
Did you guys notice the same? In Gorilla’s constructor (line 23 of code)
super is used as a “function”. Whereas, Gorilla’s showVigour() (line 35) and dailyRoutine()(line 39) methods have used super as an “object”.
The super keyword is used in two ways because of the following reasons:
In line 24, the super keyword is used as a “function” which calls the
parent class Animal with the parameters passed to Gorilla. 
This is a key step to be carried out in order to make sure that
Gorilla is an instance of Animal.
In line 36 and 40 super is used as an “object” which
refers to an Animal instance (parent class). The super keyword 
here is used to call the methods of the parent class Animal explicitly.
People familiar with languages like C#, Java, Python can pretty
much relate to how all this works. However, JavaScript was not so simple before ES6 came in, especially for classes. So how did people code without using class syntax, super and extends keywords? Or they never used such concepts before and suddenly decided to add them? Let’s find out!
*/

3.5 (2 Votes)
0
Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
javascript extends explained The extends keyword in javascript explained function extends js extends meaning javascript what does extends in javascript mean extends function js class extends in js extends function in javascript how to use extends in javascript extends keyword js what happens when extends in javascript how does extends work in js javascript __extends extends jas what is extends javascript using extends js example extends js "extends" in javascript javascript extends method extends method javascript _extends js javascript extendsion extends keyword in js js extemds extends keyword javascript javascript .extends class method extends class javascript extends extends property in javascript constructor call extend js js extending class how to extend class js javascript extend class this function extends class in javascript extend a calss js function extends class javascript do we need initialze if extens in java javascript extends in class js unextend class js uses of extends in js class extend class javasciript inline extend parent class javascript We use the extends keyword to create a subclass javascript extend javascript class which of the following keywords is used to indicate inheritance in javascript js extends option values extends java script inheritance extends and super Javascript extend from class javascript nodejs extends class extends js get javascript subclass vs extension javascript class extensions class extends dose javscript have class extends extend class constructor javascript inheritance from one clas to another class in javascript extend a class in js javascript class apply extend node js how to use function of extended class javascript "extends" method js es6 extends es6 extends extendable classes javascript ~ JavaScript class extend example js extend class need constructor what does extends do in javascript extent calsss javascript How to extend a javascript class extends in javascriot extend javascript how to extend a class in javascript extends javascritp extends keyword in javascript extending classes javascript js class properties access extend extend vs inherit javascript extends function javascript what is extend in es6 js extends function extends vs inheritance javascript extends keyword meaning in node js javascript extend a class js function extends javascript class extends not working js extend constructor function extends javascript to extend the class methods to new class in javascritp javascript extend new class $.extend in javascript javascript class extend extend in javascript class extending js class extends function javascript nodejs class extends extends in js class extends another class in javascript extend a class in javascript extend keyword javascript js class extends how to extend class in js javascript extends object javascript class extends class extedn class syntax js explain extends keyword in javascript how to extend in js how use class extends js how to extend class in nodejs what does extends mean in javascript extend class in javascript javascript extending classes extend class js extending class js javascript extends class class extend java script class extension JS how to set a class must extends another class in js must extend a class in js javascript es6 extending a class how to extend classes in javascript how to use a class extending another class javascript how exactly extend keyword works in javascript what does extends keyword in javascript class extends js javascript extands class extends in new javascript javascript extend class javascript extend class has to have method extends js extend classes javascript extend js class javascript extends with class class extends package javascript javascript class extends example class extends javascript extends class javascript class extends java script class extend javascript extended class constructor js js extend function class js extend class javascript class extends extends keyword in react why class extends objectclass jvascript extended class use method from extender javascript extended class use something from extender javascript global class extends in js js extends extend class javascript JS get extend name class javascript class extension express extend class class extends in javascript how to extned to instance of class js js child class extend class javascript es6 how does extends work in javascript extends javascript extends in class javascript javascript class and extends what is extends in javascript javascript extends extends in javascript
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source