Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

inheritance 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

javascript prototype inheritance example

function Person(first, last, age, gender, interests) {
  this.name = {
    first,
    last
  };
  this.age = age;
  this.gender = gender;
  this.interests = interests;
};

function Teacher(first, last, age, gender, interests, subject) {
  Person.call(this, first, last, age, gender, interests);

  this.subject = subject;
}
Comment

javascript inheritance

class Car {
  constructor(brand) {
    this.carname = brand;
  }
  present() {
    return 'I have a ' + this.carname;
  }
}

class Model extends Car {
  constructor(brand, mod) {
    super(brand);
    this.model = mod;
  }
  show() {
    return this.present() + ', it is a ' + this.model;
  }
}

let myCar = new Model("Ford", "Mustang");
document.getElementById("demo").innerHTML = myCar.show();
Comment

javascript inheritance

class Animal {
   null
}
class tiger extends Animal {
 null }
Comment

Inheritance in JavaScript

class teamMember {
    name;
    designation = "Support web dev";
    address;
    constructor(name, address) {
        this.name = name;
        this.address = address;
    }
}

class Support extends teamMember {
    startSession() {
        console.log(this.name, "start a support sessin");
    }
}
class StudentCare extends teamMember {
    buildARoutine() {
        console.log(this.name, "build a routine");
    }
}

const max = new Support("Max", "USA");
console.log(max);
const sarah = new StudentCare("Sarah", "UK");
console.log(sarah);
Comment

Inheritance In JavaScript


class Animal{

  constructor()
  {
    this.walk = "walk";
  }
}
class Dog extends Animal{
constructor()
{
  super();
this.bark = "bark";
}

}

const dog = new Dog();
console.log(dog.walk);
/*must include BOTH extends and super()*/
Comment

javascript inheritance

function Teacher(first, last, age, gender, interests, subject) {
  Person.call(this, first, last, age, gender, interests);

  this.subject = subject;
}
Comment

JavaScript Prototype Inheritance

// constructor function
function Person () {
    this.name = 'John',
    this.age = 23
}

// creating objects
const person1 = new Person();
const person2 = new Person();

// adding property to constructor function
Person.prototype.gender = 'male';

// prototype value of Person
console.log(Person.prototype);

// inheriting the property from prototype
console.log(person1.gender);
console.log(person2.gender);
Comment

javascript inheritance

class childClass extends parentClass
{
  
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: npx for yarn 
Javascript :: Plugin "react" was conflicted between "package.json » eslint-config-react-app 
Javascript :: how to create angular project in visual studio code windows 10 
Javascript :: pure component in react 
Javascript :: decode jwt token 
Javascript :: timer javascript 
Javascript :: class component params in react 
Javascript :: scribbletune 
Javascript :: angular get firebase firestore 
Javascript :: how to generate a random number between certain values 
Javascript :: Div draggable x axe only 
Javascript :: redux if already exist item dont add to array 
Javascript :: jquery ui dialog live cdn 
Javascript :: frame-grab js 
Javascript :: javascript check if json object is valid 
Javascript :: keyboard avoidance view not working on react native 
Javascript :: check event target jquery outside 
Javascript :: google-maps-react give undefined lat long 
Javascript :: @angular/fire has missing dependencies 
Javascript :: print array list to a ul list 
Javascript :: click on list item javascript highlight 
Javascript :: feathersjs mysql example 
Javascript :: blazor sample ujsing cliam policy 
Javascript :: Map the peoples of Ray such as their first name comes first in the string in js 
Javascript :: android studio react native plugins 
Javascript :: aktuelle session id auslesen jsf 
Javascript :: what to do when node was already in close in A* algorithm 
Javascript :: ja display snippet from text 
Javascript :: how to add set between two date in datatable 
Javascript :: get every other item in an array 
ADD CONTENT
Topic
Content
Source link
Name
1+8 =