Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

extends in javascript

// 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!
*/
Comment

extends in js

//class in es6 are just functional constructor.
//Parent class is Person and Developer class inheritant from Person class using 
//extend and super method 
class Person{
  constructor(firstname,lastname){
    this.firstname= firstname;
    this.lastname=lastname;
    
  }
  aboutPerson(){
  console.log(`My name is ${this.firstname} ${this.lastname} `)
  }
}

class Developer extends Person{
constructor(firstname,lastname,experience,projects){
 /* //super keyword is used to call the constructor
 of its parent class to access the parent's properties and methods*/
	super(firstname,lastname);
  	this.experience=experience;
  	this.projects=projects;
  
  	aboutDev(){
      console.log(`My name is ${this.firstname} and  I have ${this.experience}
	in software development`)
}
  
  const ShirshakDev= new Developer('Shirshak','Kandel',3,13)
  console.log(ShirshakDev.aboutDev())
Comment

extends in js

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.
Comment

PREVIOUS NEXT
Code Example
Javascript :: can we add two functions onclick event 
Javascript :: react hooks 
Javascript :: import slider material ui 
Javascript :: discord.js v12 how to set owner commands 
Javascript :: react navigation 
Javascript :: Number.prototype.between = function(a, b) { var min = Math.min.apply(Math, [a, b]), max = Math.max.apply(Math, [a, b]); return this min && this < max; }; 
Javascript :: check if refresh token expired redirect 
Javascript :: get file extension of path extendscript 
Javascript :: custom right click js 
Javascript :: chrome version 
Javascript :: add update react pwa feature 
Javascript :: mongoose search combine fields 
Javascript :: nodejs parallel async calls -1 
Javascript :: get row data in datatable 
Javascript :: abstract class in js 
Javascript :: js splice 
Javascript :: usestate previous state 
Javascript :: javascript bounce animation 
Javascript :: print js example 
Javascript :: algolia docs react instant search 
Javascript :: check uncek react-bootstrap-table reactjs 
Javascript :: how to use mdbreact in react js 
Javascript :: iterate over array of object javascript and access the properties 
Javascript :: nodejs: create model by mongoose package 
Javascript :: node http 
Javascript :: selectores de jquery 
Javascript :: how to create an html element in javascript without document 
Javascript :: .keys() array 
Javascript :: decode jwt token nodejs 
Javascript :: js reading file 
ADD CONTENT
Topic
Content
Source link
Name
7+6 =