inheritance in javascript

function Animal (name, energy) {
  this.name = name
  this.energy = energy
}

Animal.prototype.eat = function (amount) {
  console.log(`${this.name} is eating.`)
  this.energy += amount
}

Animal.prototype.sleep = function (length) {
  console.log(`${this.name} is sleeping.`)
  this.energy += length
}

Animal.prototype.play = function (length) {
  console.log(`${this.name} is playing.`)
  this.energy -= length
}

function Dog (name, energy, breed) {
  Animal.call(this, name, energy)

  this.breed = breed
}

Dog.prototype = Object.create(Animal.prototype)

Dog.prototype.bark = function () {
  console.log('Woof Woof!')
  this.energy -= .1
}

const charlie = new Dog('Charlie', 10, 'Goldendoodle')
console.log(charlie.constructor)

0
5

                                    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;
}

0
0
4.11
9
Yjofna 110 points

                                    class child_class_name extends parent_class_name

4.11 (9 Votes)
0
4
4
Msm 85 points

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

  this.subject = subject;
}

4 (4 Votes)
0
3
2
Lexia 125 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 (2 Votes)
0
3.8
10

                                    function Animal() { }
Animal.prototype.eat = function() {
  return "nom nom nom";
};
function Bird() { }

// Inherit all methods from Animal
Bird.prototype = Object.create(Animal.prototype);

// Bird.eat() overrides Animal.eat()
Bird.prototype.eat = function() {
  return "peck peck peck";
};

3.8 (10 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
inheritance example in js what is an inheritance in javascript what is prototype inheritance in javascript medium inheritance class js inheritance in javascript programiz javascript inheritance constructs object prototype inheritance javascript inheriting from method js es6 prototype inheritance is there js inheritance is prototype in js is inheritance prototype and prototypal inheritance in javascript functino prototype inheritance js function inheritance js inheritance prototype in javascript How inheritance works in JavaScript. inherits keyword javascript prototypes and inheritance in javascript use inheritance on methods js prototype and inheritane in js inheritance mechanism in javascript what is inheritance in javascript with example why use inheritance in javascript how to make inheritance by use prototype inherit function in javascript class inheritance in js Explain prototype inheritance in JavaScript? javascript function inheritance prototype functional programming inheritance in javascript inheritance class javascirpt inheritance in javascript support how inheritance is implemented in javascript inheritance in javascript what s does javascript have inheritense prototype inheritance in javascript yogesh acihve inheritence code javascript prototype inheritance in javascript and channing inheritance meaning javascript how is inheritance in javascript different from java how to inherit prototype in javascript prototype inheritence javascript js prototpye inheritance how to inherit function in javascript inheritance funtion in js is there inheritance is javascript inheritances in js javascript understand inheritance inheritance javascript example inheritance javascript w3schools inheritance javascript jsjunayed what in inheritence in js $(this).inherit() in javascript .inherit in javascript javascript support which inheritance when to use inheritance in js prototype inheritence in javascript inheritance in javascript w3schools inherit function js js inheritance function inherits js function javascript inheritance interface inheritance prototype js inheritance prototype functional javascript inheritance prototype constructor example javascript inheritance syntax inherit method in js DEfination of inheritance in javaScript javascript function inherit 4 ways to implement inheritance in js javascript inheritance w3schools inheritance javscript implementation inheritance javascript implement inheritance in javascript inheritance in javscript javascript inherit prototype prototype inheritance javascript function how we will apply inheritance to js prototype inheritance in javascript es6 prototype inheritance example in javascript javascript inherit functions inherit in js inheritance in vanila javascript javascript prototype inheritance w3schools create inheritance in javascript inheritance using prototype use of prototype inheritance in javascript javascript make function prototype inheritance JavaScript inheritance is What is an inheritance in JavaScript? inheritence js javascript es6 inheritance inherits in javascript what is the use of prototype inheritance in javascript javascript function inherite inheritance in class javascript what is inheritance javascript inheritance example in javscript what is the meaning of prototype in prototype inheritance in js what is the meaning of prototype inheritance in js what is class inheritance in javascript javascript prototype inheritance javascriptinfo what is inheritance hjavscript js inheritence how does inheritance work with prototypes in javascript how does inheritance work with javascript inheritance in prototype javascript inheriths javascript inheritance in js es6 function inheritance in javascript js object prototype inheritance example right way to inherit properties in javascript right way to inherit properties using prototype in javascript protypial inheritance javascript javascript inheritance js inherit prototype from prototype inheritance object.create how to do prototype inheritance in javascript 3 ways create inheritance in javascript Simple JavaScript Inheritance inheritance in javascript vs java type of inheritance in javascript what is prototype and prototype inheritance in javascript javascript prototype inheritance definition inheritance in javascript with example inherit javscript function inherit javascript how ot use inheritance in js why to use inheritance in js javascrip inheritance how to do inheritance with prototype in js how does prototype inheritance work How to implement inheritance in JavaScript what inheritance does javascript follows inheritance in plain javascript javascript prototype constructor inheritance types of inheritance in javascript implementing inheritance in JS js inheritance prototype prototype based inheritance in javascript what is JavaScript inheritance inheritance with functions js inheritance in javascipt javascript prototype.call inheritance model inheritance in javascript how to use inheritance in javascript inheritence javascript prototype inheritance js definition inheritance in javascript prototype javascript inheritacne is javascript support inheritance what is inheritance js js inheritace explain prototypal inheritance in javascript method inheritance java script js inherit properties basic inheritance js javascript object inheritance prototype javascript oop inheritance prototype Inheritor js inherit js interface inheritance in javascript How the Inheritance in Javascript works javascript prototype inheritance model what is inheritance in javascript javascript function inheritance prototype class inheritance js what is the purpose of inheritance in javascript how to apply inheritance in javascript inheritance in constructor javascript javascript inheritance concept inheritance in js class inheritance in javascript js inheritance example code inheritance in node js does javascript support inheritance js inheritance w3 javascript inheritance es6 prototype based inheritance javascript how to demonstrate inheritance in javascript javascript inheritance example code javascript how to inheritance inheritance in javascript es6 inheritance definition in javascript w3schools javascript inheritance javascript prototypes and inheritance inheritance concept in javascript inherentance js inheritance in javascript from other function constructor inheritance in javascript without call inheerent a constructro in javascript inherent a constructor javascript inherint a object in javascript prototypal inheritance javascript example javascript object prototype chain class.prototype javascript purpose of inheritance in js javascript extend with prototype prototyp javascript inheritance in javascript onject object inherit from in javascript js inheritance es6 function class inheret js inherit prototype javascript constructor extend js prototype inheritance vs class inheritance prototype inheritance in javacript inheritance is accomplice in javascript thriugh the use of which construct multiple inheritance in js inheritance vs extension javascript javascript date object prototype chain node js can object inherite object inheritence in javascript how can you inherit properties from other objects in javascript javascript creating object with separate prototypes Inheritance Between "Classes": Constructor Functions in javascript inherit in javascript can you go and see all prototypes inheriting from a prototype in js? how to get contructor variables in child object what is relation between function and object in terms of prototypal how and why to create and use your own objects, as well as specialization using inheritance. javascript javascript prototype do we need initialze if extens in java javascript prototypal inheritence js why 'this' inherintece of object in js how to change value of protype in js javascript manipulate prototype object inheritance in javascript in line assignment of parent class javascript javascript inheritance method How to complete a inherited method in jS How to complete a inherited function in jS Heritance JS Cascading prototypal inheritance js how to call a function in object prototype inheritance in javascript complex properties inheritance in javascript large number properties javascript inheretence example prototype chaining javascript inharitance in javascript how to inherit constructor javascript javascript why use prototype prototype language javascript javascript extend prototype this is empty object prototype inheritance in javascript super method js for in into prototype node js prototype any some one object property inharit javascript js function prototype javascript constructor inheritance what is prototypal inheritance what is javascript prototype /mnt/c/Users/steph/OneDrive/Desktop/aA_classwork/W9D1/W9D1/inheritance/inheritance.js inheritance injs what is a prototype chain for method sharing prototype and prototype inheritance js objects & prototypal inheritance make none inherited class values in javascript inhertiance in javascript prototypal inheritance js object inheritance concepts javascript how does class inheritance work javascritp javascript in inheritance takes place by cloning prototype set constructor as parent in prototype chain what is prototype based inheritance inheritance with javascript function javascript all objects share the same prototype Prototyical Inheritance prototy inheritance javascript prototype example js prototype method prototy javascript What does 'inherited from base Object' in JS mean? types of class inheritance in javascript js function inheritance js runtime prototypes What is inheritance? in js inheritance in javascirpt javascriptCreate three new subclasses that inherit from the Employee parent class above inheritance js syntax javascript Inheritance_and_the_prototype_chain visually explained inherit a property js from a class inherit a property js javascript protypal inheritance All Javascript objects inherit their properties and methods from their prototype. Group of answer choices inheretance in js prototype chaining inheritance Prototypes, inheritance js oop prototype javascript interit specific properties extend prototype javascript should i use prototype inheritance in javascript subclass don't inherit all properties javascript js add prototype to default classes What is prototype inheritance? restrict the variable access in js inheritance protypal inheritance in js what is prototypal inheritance prototype in javascript inheritance Prototypal Inheritance, Understanding the JavaScript Prototype Chain & Inheritance | @RisingStack javascript prototype method assignment how to inherit in javascript javascript prototype chain prototype and prototype chaining in javascript javascript prototype inheritance constructor inheirts function j Prototypal Inheritance: exapmple prototype based inheritance javascript hereditary js how to inherit objects o.prototype.extends inheritance function js node js inheritance function inheritance with methods javasc this.prototype how to subclass when prototyping in js What object do all other object data types inherit from js? js create child class object js create object of type child class what is prototype inheritance prototypal inheritance javacript.info javascript inherit function parent class that holds manager and users jvascript what is inheritance and prototype in js sharing method using protoype in javscript javascript inheritance why set construtor how to use class initial properties using chiled class javascript inheriatance in Js function.prototype.inherits function prototype inheritance javascript inhetance and the prototype chain in js inheritance jvascript inheritence in js js extend constructor inheritance in javascript example protopla inheritance js prototype extend function prototype in inheritance prototype inside prototype js extend prototype best way to prototype oriented javascript inheritance example in javascript does javascript have inheritance prototype chain inheritance in java script inheritance oop javascript diagram of prototypal inheritance javascript how to create inherit and prototype javascript prototype chain illustrated javascript prototypal inheritance using prototype to change function behaviour javascript prototypal inheritance in js inheritance is accomplished in js by which constructoe how to accomplish inheritance in javascript javascript inheritence prototype for a class js prototype inheritance example proto chain in javascript javascript extend prototype how to give prototype inheritance in javascript get all prototype class for other class javascript what is prototype inheritance in javascript js inherit object how to achieve inheritance in javascript javascript prototype inheritence why object not support prototype in js prototype and inheritance in javascript inheritance javscript javascript prototype extend object level prototype leve; class level js use function to inherit prototype inhweritace prototype inheritence javascript inheritance constructor js prototype add many functions inheritense in js javascript inheritance tutorial type a type b type c inheritance is possible in javascript js inheritance methods js design inheritance what is prototypal inheritance in javascript how javascript implement prototype inhreitance prototype based objects inheritance how to inherit in js class inherit only some attributes javascript inheritance in javascrip modern javascript classes introduce powerfull inheritance inherit from other object in js inherit from other class csser object in js convert js into oojs online creating an inherited object js function this inheritance javascript oop subclasses javascript inheritance inherent javascript inheritance mu loop inherit javascript object javascript class prototype inheritance javascript function objects inheritance inheritance in javascript functions javascript inheritance example js modify prototype of variable A inherits b javascript when should inheritance be used in javascript js object how to inherit from another object how to inherit property of function in to another js class prototype with function prototype javascript check from top level prototype javascript prototype chain code with me example javascript prototype chain example javascript syntax to create a new class that inherits property feature helps us to simplify the inheritance concept. Modules Child Classes Functions _________ feature helps us to simplify the inheritance concept. Classes Functions Child Modules protypal js js prototype inheritence is js prototype inheritence how to get ingeritance in js how adding a property to the prototype is beneficiary in this case then adding it directly to the constructor function? prototype inheritance in js javascript prototypical inheritance inherting in javascript extends in prototype javascript javascript, prototypal inheritance, If we look for a property in rabbit, and it’s missing, JavaScript automatically takes it from animal. create child type javascript examples of inheritance in JS js object inheritance js inheritance javascript object inheritance implementing class prototype js How many prototype objects are in the chain for the [] array How many prototype object are in the chain for the [] array inheritance js js inherit destroy javascript object constructor inheritance class prototype javascript object prototype extend javascript javascript oop inheritance prototype chaining in javascript prototype inheritance example prototypical inheritance in javascript inheritance u javascriptu how to implements from parent js will the property object inherit everythin from orinal object how to set prototype in higher prototypes js how to create a subclass with functional javascript multiple objects that have access to the same prototype why you can access childs methods in prototype js inherintance in js inheritance in object in javascript function in a class prototype property prototype inheritance in depth javascript how many prototype objects are in the chain for the following array prototype javascript object manipulation javascript inheritance using prototype Practice creating objects Object methods, classes Inheriting from a parent class javascript student.parent = Person; javascript javascript create non inherited method ways to inherit prototypes javascript prototypal inheritance javascript beginners Identify what type of inheritance mechanism is used by Javascript js inheritance example js constructor inheritance inherits from the constructor function's prototype Creates an empty object (which we'll call instance) which prototypal inherits from Constructor.prototype Creates an empty object (which we'll call instance) which prototypally inherits from Constructor.prototype how many prototypeobjects are int the chain for the following array js prototypal inheritance how to inherit right key functionality in javascrupt prototypal inheritance in javascript JavaScript OOP hasOwnProperty() inherits from the constructor function prototype javascript subclasses inherit protorypes js javascript prototype inheritance example inherit function in function constructor javascript inheritance in JS. prototypical inheritence javascri[t js inheritance prototype function node js prototype inherits library objext inheritance in javascript prototypical inheritance in js object inheritance javascript javascript extending a class prototype with props and methods javascript derived object sign for all derived clases in javascript how to write every derived class in java script javascript prototypal inheritance inheritance constructor js javascript inherits extend a function js prototype how to get function to inherit another function in js how to inherit 'this' javascript how to inherit this javascript javascript chain traverse prototype redefine object prototype javascript Create three new subclasses that inherit from the Employee parent class above. in javascript assign a value to subclass javascript what is javascript prototype inheritance How many prototype objects are in the chain for the following array? prototype-inheritance prototypical inheritance using prototyp js with class prototype javascript inheritance extend prototpes javascript inheritace js inheritance in js prototype object chain in array javascript how many prototype objects are in the chain for the following arary? protypal inhertiance javscript constructor function subclassing javascript how to inherit prototype javascript js prototype chain Prototypal JavaScript prototypes can inherit from only 1 other prototype. prototypal inheritance javascript javascript extend module via prototype javascript extends prototype prototypal inheritance .length how do you use a function in prototypal inheritance javascript prototype inheritance inheritance using prototype in javascript prototype javascript inheritance in javascript javascript inheritance javascript inheritance prototype prototype inheritance in javascript example object prototype chain prototype inheritance in javascript inheritance javascript EXplain how this works in JavaScripExplain how prototypal inheritance works. Can you give an example of one of the ways that working with this has changed in ES6? js prototype inheritance inheritance javascript prototype prototypical inheritance javascript prototype inheritance in JavaScript? javascript inherit methods from another object javascript prototype inheritance javascript instance prototype prototype inheritance 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