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

0
6
Ken Daniel 145 points

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

0
0
4.5
6
Tonyk4316 95 points

                                    function inherit(c, p) {
        Object.defineProperty(c, 'prototype', {
            value: Object.assign(c.prototype, p.prototype),
            writable: true,
            enumerable: false
        });

        Object.defineProperty(c.prototype, 'constructor', {
            value: c,
            writable: true,
            enumerable: false
        });
    }
// Or if you want multiple inheritance

function _inherit(c, ...p) {
        p.forEach(item => {
            Object.defineProperty(c, 'prototype', {
                value: Object.assign(c.prototype, item.prototype),
                writable: true,
                enumerable: false
            });
        })

        Object.defineProperty(c.prototype, 'constructor', {
            value: c,
            writable: true,
            enumerable: false
        });
    }

4.5 (6 Votes)
0
5
1
Banhfun 85 points

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

  this.subject = subject;
}

5 (1 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 prototype inheritance example inheritance class js inheritance in javascript programiz javascript inheritance constructs inheriting from method js is there js inheritance function inheritance js How inheritance works in JavaScript. inherits keyword javascript use inheritance on methods js inheritance mechanism in javascript what is inheritance in javascript with example why use inheritance in javascript class inheritance in js 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 acihve inheritence code javascript inheritance meaning javascript how is inheritance in javascript different from java 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 inheritance in javascript w3schools inherit function js js inheritance function inherits js function javascript inheritance interface 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 how we will apply inheritance to js javascript inherit functions inherit in js inheritance in vanila javascript inherit function in javascript JavaScript inheritance is What is an inheritance in JavaScript? inheritence js javascript es6 inheritance inherits in javascript javascript function inherite inheritance in class javascript what is inheritance javascript inheritance example in javscript what is inheritance hjavscript js inheritence how does inheritance work with javascript inheritance in prototype javascript inheriths javascript inheritance in js es6 function inheritance in javascript right way to inherit properties in javascript javascript inheritance 3 ways create inheritance in javascript Simple JavaScript Inheritance inheritance in javascript vs java type of inheritance in javascript javascript prototype inheritance definition inheritance in javascript with example inherit javscript function how ot use inheritance in js why to use inheritance in js javascrip inheritance How to implement inheritance in JavaScript what inheritance does javascript follows inheritance in plain javascript types of inheritance in javascript implementing inheritance in JS what is JavaScript inheritance inheritance with functions js inheritance in javascipt model inheritance in javascript inheritance in javascript prototype javascript inheritacne is javascript support inheritance what is inheritance js js inheritace method inheritance java script js inherit properties basic inheritance js Inheritor js inherit js interface inheritance in javascript How the Inheritance in Javascript works what is the purpose of inheritance in javascript how to apply inheritance in javascript inheritance in constructor javascript javascript inheritance concept inheritance in js js inheritance example code inheritance in node js inherit javascript does javascript support inheritance js inheritance w3 javascript inheritance es6 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 inheritance concept in javascript inherentance js inheritance in javascript from other function constructor inheritance in javascript without call prototypal inheritance javascript example inheerent a constructro in javascript inherent a constructor javascript inherint a object in javascript javascript object prototype chain class.prototype javascript If we want a member of a prototype of the same class how can we do that 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 constructor extend js prototype inheritance in javacript inheritance is accomplice in javascript thriugh the use of which construct multiple inheritance in js inheritance vs extension javascript prototype inheritance in js javascript date object prototype chain node js can object inherite object inheritence in javascript inheritance keyword in javascript how can you inherit properties from other objects in javascript inheritance constructor functions js Inheritance Between "Classes": Constructor Functions in javascript can you go and see all prototypes inheriting from a prototype in js? how to inherite class variables from in js how to get contructor variables in child object javascript how to inherit object from another class how and why to create and use your own objects, as well as specialization using inheritance. javascript 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 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 how to inherit constructor javascript javascript inherit calss javascript extend prototype this is empty object node js prototype any some one javascript constructor inheritance can you inherit from this function based "class" in js what is prototypal inheritance inheritance injs prototypal inheritance javascript objects & prototypal inheritance make none inherited class values in javascript inhertiance in javascript object inheritance concepts javascript how does class inheritance work javascritp javascript inheritance without classe javascript in inheritance takes place by cloning prototype what is prototype based inheritance overview of inheritance in js function javascript all objects share the same prototype inheritance using prototype in javascript prototypal inheritance in js What does 'inherited from base Object' in JS mean? types of class inheritance in javascript js runtime prototypes What is inheritance? in js 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 javascriupt array of inherited objecs javascript protypal inheritance inheretance in js what is inheritance js? js oop prototype can inheritance go both ways js class js inherit javascript interit specific properties can we create inheritance javascript extend prototype javascript subclass don't inherit all properties javascript js add prototype to default classes js prototype doesnt tranform into method how can we access prototype of same class in another function restrict the variable access in js inheritance javascript prototype method assignment how to inherit in javascript prototype and prototype chaining in javascript js add prototypes to native classes javascript prototype inheritance constructor inheirts function j javascript how to test function of inheritance javascript hereditary js how to inherit objects o.prototype.extends inheritance function js javascript functions to be used inheritance node js inheritance this.prototype class and subclass in javascript example javascript class inh class constructor function example subclass inheretance javascript javscript classes how to choose which functions to inherit how to subclass when prototyping in js force class to be generate in prototype inheritance class javascript What object do all other object data types inherit from js? js create child class object js create object of type child class function inheritance in constructor implementation inheritance JS parent class that holds manager and users jvascript sharing method using protoype in javscript sub class in js javascript why fix constructor property after inhjeritance javascript inheritance different construtor javascript inheritance why set construtor how to use class initial properties using chiled class javascript inheriatance in Js function prototype inheritance javascript prototypal inheritance inhetance and the prototype chain in js inheritance jvascript javascript inherit params from child class js how to inheritance a class inheritence in js js extend constructor inheritance in javascript example protopla inheritance js prototype extend function prototype inside prototype js extend prototype inheritance in js classes best way to prototype oriented javascript inheritance example in javascript does javascript have inheritance parent constructor example in js inheritance not working js function inherits this inheritance in java script in heritance in js inheritance oop javascript how to create inherit and prototype javascript how to create inheritance and constructor in javascript using prototype to change function behaviour javascript classes and inheritance in javascript how to inheret class javascript inherit JS constructor method how to inherit object methods from another object constructor without object classes how to inherit object methods from another object constructor inheritance is accomplished in js by which constructoe how to accomplish inheritance in javascript prototype for a class proto chain in javascript javascript extend prototype subclass inheritance javascript how to give prototype inheritance in javascript inheritance example javascript get all prototype class for other class javascript js inherit object how to achieve inheritance in javascript javascript prototype inheritence why object not support prototype in js inheritance javscript class inheritance method javascript javascript prototype extend object level prototype leve; class level class inheritance js js use function to inherit javascript inheritance class javascript inheritance single parent javascript inheritance constructors javascript inheritance constructor how to do inheritance of class javascript js prototype add many functions inheritense in js javascript inheritance tutorial js use functions as classes inheritance prototype inheritance type a type b type c inheritance is possible in javascript javascript inherit class js inheritance methods js design inheritance prototype based objects inheritance how to inherit in js class inherit only some attributes javascript inheritance in javascrip javascript inherit object what is class inheritance in javascript 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 js function inheritance javascript oop subclasses javascript inheritance inherent javascript inheritance mu js object child extend class javascript inheritence javascript inheritence js inherit method js inherit methods intermediate function inheritance javascript loop inherit javascript object javascript class prototype inheritance javascript function objects inheritance 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 prototypical inheritance in js javascript check from top level prototype javascript prototype chain code with me 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 Which keyword can be used to implement inheritance in ES6? inherits extends derives implements Which keyword can be used to implement inheritance in ES6? extends derives implements inherits protypal js how to get ingeritance in js a sub b superclass inhertence javascript prototype inheritance javascript js class inheritance achieve inheritance in javascript 2 ways how adding a property to the prototype is beneficiary in this case then adding it directly to the constructor function? inherit in javascript what is inheritence in JS inherting in javascript how to extend prototype of A object to B object js extends in prototype javascript create child type javascript how do i inherit function constructor in javascript examples of inheritance in JS javascript object instance extends class js object inheritance why do parent prototype has properties of child object 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 how to inherit class in javascript inheritance js js inherit destroy inheritance js classes inherits from class and implement class constructor javascript class prototype javascript object prototype extend javascript javascript oop inheritance how to inherit value in front end javascript prototype chaining in javascript can a javascript class inherit from another class javascript extend class manually set parent instance javascript child class set parent using getter in child class javascript inheritance u javascriptu how to implements from parent js will the property object inherit everythin from orinal object prototypal inheritance in javascript how to set prototype in higher prototypes js how to create a subclass with functional javascript why you can access childs methods in prototype js inherintance in js inheritance in object in javascript function in a class prototype property do i need to instansite the subclass with the base class args javasacript do i need a constructor in javascript when inheariting in javascript everything inherits from object javascript derived class create inheritance class in javascript create inheritance in javascript how many prototype objects are in the chain for the following array prototype javascript object manipulation how to inherit a class in javascript how to use inheritance in javascript Practice creating objects Object methods, classes Inheriting from a parent class javascript javascript function inheritance values inherit from another class javascript student.parent = Person; javascript javascript create non inherited method create object method not inherited ways to inherit prototypes javascript inherit class javascript Identify what type of inheritance mechanism is used by Javascript inheriting classes in javascript r setting up prototype inheritance between objects in javascript how to extend subclass from another subclass 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 JavaScript OOP hasOwnProperty() inherits from the constructor function prototype javascript subclasses inherit protorypes js javascript inheritance prototype inherit function in function constructor javascript javascript class inheritance inheritance in JS. inherit class js inherent class js classes inherit from other classes javascript class and inheritance in javascript prototypical inheritence javascri[t js inheritance prototype function js inheritance prototype node js prototype inherits library inheritance javascript classes objext inheritance in javascript class inheritance in javascript javascript extending a class prototype with props and methods js classes inheritance herencia es extender javascript must a derived class have a constructor in javascript derived class javascript javascript derived object sign for all derived clases in javascript how to write every derived class in java script array how many prototype pbj are in the chain for js inheritance 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 inherit methods class inheritence javascript javascript chain traverse prototype redefine object prototype javascript access value from inherited object javascript Create three new subclasses that inherit from the Employee parent class above. in javascript assign a value to subclass javascript 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 prototype inheritance in javascript inheritance class in js inheritance in javascript functions prototype object chain in array javascript how many prototype objects are in the chain for the following arary? constructor function subclassing javascript how to create inheritance in javascript how to inherit prototype javascript javascript extend inherits javascript inherits js prototype chain JavaScript prototypes can inherit from only 1 other prototype. javascript object constructor inheritance how to copy inherited attributes javascript object inheritance javascript what is inheritance in javascript object inheritance js javascript extend module via prototype es6 class extend prototype classes inheritance javascript javascript extends prototype define inherirance in js prototypal inheritance .length how object inherit methos from constructor in javascript don't understand is how and why objects can 'inherit' methods from the constructor. javascript javascript element inherents class how do you use a function in prototypal inheritance javascript class inheritance javascript javascript inheritance in javascript function inherits from object remove class inheritance from an object in js inheritance javascript inheritance in javascripots inheritence javascript javascript inheritance and new operator objects are extended by adding new methods to those object’s prototypes. You should start at the top and move down to the lower subclasses to define the _____. a. prototype chain b. object constructor c. base object d. object literal how prototypal inheritance works. Can you give an example of one of the ways that working with this has changed in ES6? Inherticance javascript js prototype inheritance prototypical inheritance javascript javascript inherit methods from another object javascript prototype inheritance _inherits js inhertinace in js Inheritance using JavaScript herencia prototype javascript javascript function inherits from object inheritance in javascript js how to get inheritance chain of a class js protypal inheritance inheritance in js javascript inherit properties from object js call inherit properties js methods not inherited from proto inheritance in javascript javascript inherit function
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