how to create subclasses 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!
*/
Also in JavaScript:
- Title
- angular npm angular material
- Category
- JavaScript
- Title
- get query parameters in node.js
- Category
- JavaScript
- Title
- adding delay in javascript foreach loop
- Category
- JavaScript
- Title
- how to create response time router node js
- Category
- JavaScript
- Title
- check if string is datestring javascript
- Category
- JavaScript
- Title
- componentwillreceiveprops hooks
- Category
- JavaScript
- Title
- forever loop in js
- Category
- JavaScript
- Title
- checked unchecked through js
- Category
- JavaScript
- Title
- javascript arrow function
- Category
- JavaScript
- Title
- assign freemarker expressions to variables
- Category
- JavaScript
- Title
- comment obtenir l'élément parent javascript
- Category
- JavaScript
- Title
- disable mouse right click javascript
- Category
- JavaScript
- Title
- for Loop continue
- Category
- JavaScript
- Title
- get utc time javascript
- Category
- JavaScript
- Title
- check element exist in jquery
- Category
- JavaScript
- Title
- angular bootstrap not working
- Category
- JavaScript
- Title
- javascript add nd st th to number
- Category
- JavaScript
- Title
- capitalize all letters jquery
- Category
- JavaScript
- Title
- javascript check if string contains substring
- Category
- JavaScript
- Title
- fill array with values javascript
- Category
- JavaScript
- Title
- enzyme test method
- Category
- JavaScript
- Title
- array.filter in js
- Category
- JavaScript
- Title
- add select option jquery
- Category
- JavaScript
- Title
- data-widget="pushmenu" collable automatically
- Category
- JavaScript
- Title
- how to validate input type=date in javascript
- Category
- JavaScript
- Title
- array data structure javascipt interview questions
- Category
- JavaScript
- Title
- getrect javascript
- Category
- JavaScript
- Title
- bootstrap 4 form validator with jquery
- Category
- JavaScript
- Title
- javascript add function to onchange event
- Category
- JavaScript
- Title
- get window height javascript
- Category
- JavaScript
- Title
- how to clear terminal in vs code
- Category
- JavaScript
- Title
- array in javascript
- Category
- JavaScript
- Title
- alternative for componentdidmount
- Category
- JavaScript
- Title
- html external javascript
- Category
- JavaScript
- Title
- install vue-cli
- Category
- JavaScript
- Title
- eval in js
- Category
- JavaScript
- Title
- enable disable click on div jquery
- Category
- JavaScript
- Title
- get image file width javascript
- Category
- JavaScript
- Title
- foreach javascript
- Category
- JavaScript
- Title
- how to convert whole strig in lowercase in js
- Category
- JavaScript
- Title
- angular sanitize vs validators
- Category
- JavaScript
- Title
- change class javascript
- Category
- JavaScript
- Title
- array of array key value javascript
- Category
- JavaScript
- Title
- angular filter ngfor
- Category
- JavaScript
- Title
- hackerrank fizzbuzz javascript
- Category
- JavaScript
- Title
- How do I get Month and Date of JavaScript in 2 digit format
- Category
- JavaScript
- Title
- append new element for each value in array d3.js
- Category
- JavaScript
- Title
- in javascript how to split string
- Category
- JavaScript
- Title
- how to get a toggle button to do different js functions
- Category
- JavaScript
- Title
- how to use the map method in javascript
- Category
- JavaScript
- Title
- angular run behind proxy
- Category
- JavaScript
- Title
- javascript class click event
- Category
- JavaScript
- Title
- format date js
- Category
- JavaScript
- Title
- add element to array javascript
- Category
- JavaScript
- Title
- how will it look when there is a container inside a a row bootstrap
- Category
- JavaScript
- Title
- how to inject service in component angular 6
- Category
- JavaScript
- Title
- how to draw a flower in javascript
- Category
- JavaScript
- Title
- how to use session using javascript
- Category
- JavaScript
- Title
- click point invert zoom react simple map
- Category
- JavaScript
- Title
- dummy api json
- Category
- JavaScript
- Title
- dropzone add download button
- Category
- JavaScript
- Title
- how to set text for label in jquery
- Category
- JavaScript
- Title
- how to return an object in javascript
- Category
- JavaScript
- Title
- airbnb and eslint react native
- Category
- JavaScript
- Title
- Javascript case insensitive string comparison
- Category
- JavaScript
- Title
- casl react
- Category
- JavaScript
- Title
- get all a tags javascript
- Category
- JavaScript
- Title
- find method javascript
- Category
- JavaScript
- Title
- grepper not showing
- Category
- JavaScript
- Title
- firestore set a document
- Category
- JavaScript
- Title
- import react
- Category
- JavaScript
- Title
- angular routing url params
- Category
- JavaScript
- Title
- deparam javascript
- Category
- JavaScript
- Title
- append after element jquery
- Category
- JavaScript
- Title
- get all the child of the same class javascript
- Category
- JavaScript
- Title
- how to wait for an exec command to fininsh in nodejs
- Category
- JavaScript
- Title
- error metro bundler process exited with code 1 react native
- Category
- JavaScript
- Title
- convert date to string javascript
- Category
- JavaScript
- Title
- javascript array to comma separated string
- Category
- JavaScript
- Title
- how to attach javascript to html
- Category
- JavaScript
- Title
- how to detect a button click in javascript
- Category
- JavaScript
- Title
- arrow function javascript ie11
- Category
- JavaScript
- Title
- immediate invoke function js
- Category
- JavaScript
- Title
- include jquerty
- Category
- JavaScript
- Title
- javascript button onclick programmatically
- Category
- JavaScript
- Title
- Javascript compare two arrays
- Category
- JavaScript
- Title
- floor in js
- Category
- JavaScript
- Title
- import jquery in angular
- Category
- JavaScript
- Title
- adjust() js
- Category
- JavaScript
- Title
- express server replit
- Category
- JavaScript
- Title
- how to use chart js in vue js
- Category
- JavaScript
- Title
- access angular app outside localhost
- Category
- JavaScript
- Title
- divide intagers javascript
- Category
- JavaScript
- Title
- An unhandled exception occurred: Script file ../node_modules/jquery/dist/jquery.min.js does not exist.
- Category
- JavaScript
- Title
- how to create 3d gallery using javascript
- Category
- JavaScript
- Title
- javascript check if file exists in folder
- Category
- JavaScript
- Title
- javascript change url hash
- Category
- JavaScript
- Title
- concat js mdn
- Category
- JavaScript
- Title
- check if class is active jquery
- Category
- JavaScript
- Title
- hide show div jquery
- Category
- JavaScript