inheritance in javascript
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!
*/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
});
}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)
Also in JavaScript:
- Title
- how to version a react app azure pipelines
- Category
- JavaScript
- Title
- count duplicates array js
- Category
- JavaScript
- Title
- hover vanilla javascript
- Category
- JavaScript
- Title
- Javascript check for hash in URL
- Category
- JavaScript
- Title
- check array for duplicate values javascript
- Category
- JavaScript
- Title
- console.log('Hi there!');
- Category
- JavaScript
- Title
- how to add onclick to child element created javascript
- Category
- JavaScript
- Title
- how to get variable in local storage in javascript
- Category
- JavaScript
- Title
- how to add javascript to html
- Category
- JavaScript
- Title
- how to empty form after submit react
- Category
- JavaScript
- Title
- .push js
- Category
- JavaScript
- Title
- how to use ternary operatiion in sequelize join statement
- Category
- JavaScript
- Title
- get all a tags javascript
- Category
- JavaScript
- Title
- email regex javascript
- Category
- JavaScript
- Title
- angular transition animation
- Category
- JavaScript
- Title
- angular for loop
- Category
- JavaScript
- Title
- how to import jquery in js file
- Category
- JavaScript
- Title
- how to make input type date supported on all browsers
- Category
- JavaScript
- Title
- functional component react
- Category
- JavaScript
- Title
- how to map over arrays vuejs
- Category
- JavaScript
- Title
- file input disable open file picker javascript
- Category
- JavaScript
- Title
- how to open a new html page on button click in javascript
- Category
- JavaScript
- Title
- error Invalid regular expression: /(.*\\__fixtures__\\.*|node_modules[\\\]react[\\\]dist[\\\].*|website\\node_modules\\.*|heapCapture\\bundle\.js|.*\\__tests__\\.*)$/: Unterminated character class.
- Category
- JavaScript
- Title
- get text of selected option jquery
- Category
- JavaScript
- Title
- An unhandled exception occurred: Script file ../node_modules/jquery/dist/jquery.min.js does not exist.
- Category
- JavaScript
- Title
- access session data from ejs view
- Category
- JavaScript
- Title
- include gif in react
- Category
- JavaScript
- Title
- assign values to a table from javascript in VF page
- Category
- JavaScript
- Title
- como ler um arquivo json com javascript
- Category
- JavaScript
- Title
- angular one way property binding
- Category
- JavaScript
- Title
- how to get a toggle button to do different js functions
- Category
- JavaScript
- Title
- arrow function in javascript w3schools
- Category
- JavaScript
- Title
- express fingerprint
- Category
- JavaScript
- Title
- how to make a div scrollable
- Category
- JavaScript
- Title
- check window resize javascript
- Category
- JavaScript
- Title
- geolocation speed
- Category
- JavaScript
- Title
- get the size of the screen javascript
- Category
- JavaScript
- Title
- Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project electronicbookshop: Compilation failure
- Category
- JavaScript
- Title
- get date one week from now javascript
- Category
- JavaScript
- Title
- how to add two number using jqueryu
- Category
- JavaScript
- Title
- allow cross origin node
- Category
- JavaScript
- Title
- go to another page javascript
- Category
- JavaScript
- Title
- how to print an array javascript
- Category
- JavaScript
- Title
- how to delete an element from an array in javascript
- Category
- JavaScript
- Title
- error duplicate resources react native
- Category
- JavaScript
- Title
- how to filter object in javascript
- Category
- JavaScript
- Title
- foreach w3schools javascript
- Category
- JavaScript
- Title
- async iife
- Category
- JavaScript
- Title
- how to add class to element on mouseclick with js
- Category
- JavaScript
- Title
- event target javascript
- Category
- JavaScript
- Title
- create array with specific size javascript
- Category
- JavaScript
- Title
- image continuous changing div
- Category
- JavaScript
- Title
- insert into specific array index that has a value existing on that index
- Category
- JavaScript
- Title
- how to create a variable in javascript
- Category
- JavaScript
- Title
- ajax request qml
- Category
- JavaScript
- Title
- get and set es6
- Category
- JavaScript
- Title
- access index of array javascript
- Category
- JavaScript
- Title
- call javascript function after page load complete
- Category
- JavaScript
- Title
- graal.js javascript array in java
- Category
- JavaScript
- Title
- download jquery
- Category
- JavaScript
- Title
- how to get element by title js
- Category
- JavaScript
- Title
- how to delete element in list javascript
- Category
- JavaScript
- Title
- how to do a classname variable and string react
- Category
- JavaScript
- Title
- how to get the square root in js
- Category
- JavaScript
- Title
- add two numbers javascript
- Category
- JavaScript
- Title
- base64 decode javascript
- Category
- JavaScript
- Title
- change src of iframe jquery
- Category
- JavaScript
- Title
- how to delete a variable in js
- Category
- JavaScript
- Title
- for of loop javascript
- Category
- JavaScript
- Title
- all ajaxcomplete event
- Category
- JavaScript
- Title
- how to square a value in javascript
- Category
- JavaScript
- Title
- how will it look when there is a container inside a a row bootstrap
- Category
- JavaScript
- Title
- how to push array
- Category
- JavaScript
- Title
- angular onchange event
- Category
- JavaScript
- Title
- iterate over array of object javascript and access the properties
- Category
- JavaScript
- Title
- heroku failed to run files from static folder in node
- Category
- JavaScript
- Title
- how to call javascript method using selectlist on change in vf page
- Category
- JavaScript
- Title
- iterate object js
- Category
- JavaScript
- Title
- bubble sort
- Category
- JavaScript
- Title
- hello world javascript
- Category
- JavaScript
- Title
- javascript assertequals
- Category
- JavaScript
- Title
- how to use current data in javascript
- Category
- JavaScript
- Title
- for in javascript
- Category
- JavaScript
- Title
- how to set height of material ui dialog react
- Category
- JavaScript
- Title
- how to make a show password button
- Category
- JavaScript
- Title
- best way to filter table in angular
- Category
- JavaScript
- Title
- how to change node version
- Category
- JavaScript
- Title
- get selected text js
- Category
- JavaScript
- Title
- en eternal gloden braid
- Category
- JavaScript
- Title
- iframe reload parent page
- Category
- JavaScript
- Title
- how to use the foreach method in javascript
- Category
- JavaScript
- Title
- angular moment pipe timeAgo
- Category
- JavaScript
- Title
- bootstrap javascript cdn
- Category
- JavaScript
- Title
- javascript base 10 to base 2
- Category
- JavaScript
- Title
- How do I check if an element is hidden in jQuery?
- Category
- JavaScript
- Title
- javascript access php variable
- Category
- JavaScript
- Title
- how to open bash_profile
- Category
- JavaScript
- Title
- how to clear radio field in jquery
- Category
- JavaScript
- Title
- equal to or more than javascript
- Category
- JavaScript
- Title
- angularjs make post request
- Category
- JavaScript