extends 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
- how to make vue app talk to backend on 8000
- Category
- JavaScript
- Title
- discord.js
- Category
- JavaScript
- Title
- javascript check image src
- Category
- JavaScript
- Title
- contains substring javascript
- Category
- JavaScript
- Title
- array length for boolean in javascript
- Category
- JavaScript
- Title
- copy one array to another javascript
- Category
- JavaScript
- Title
- google maps api javascript
- Category
- JavaScript
- Title
- How can I check whether a variable is defined in Node Js
- Category
- JavaScript
- Title
- how to reload vscode
- Category
- JavaScript
- Title
- how to append in javascript
- Category
- JavaScript
- Title
- .includes javascript
- Category
- JavaScript
- Title
- box shadow javascript style change
- Category
- JavaScript
- Title
- apache log format json
- Category
- JavaScript
- Title
- bind jquery trough name
- Category
- JavaScript
- Title
- electron jquery
- Category
- JavaScript
- Title
- Appending the option element using jquery each function
- Category
- JavaScript
- Title
- a simple javascript calculator
- Category
- JavaScript
- Title
- ERROR in ./node_modules/tns-core-modules/ui/core/view/view.js Module not found: Error: Can't resolve '@nativescript/core/ui/core/view/view' in '/home/deepali/projects/supersquad-client/node_modules/tns-core-modules/ui/core/view'
- Category
- JavaScript
- Title
- chai js
- Category
- JavaScript
- Title
- indexing string in javascript
- Category
- JavaScript
- Title
- add readonly attribute jquery
- Category
- JavaScript
- Title
- check if variable is undefined or null jquery
- Category
- JavaScript
- Title
- event.stoppropagation
- Category
- JavaScript
- Title
- how to access global scope in to local scope using javascript examples
- Category
- JavaScript
- Title
- graphql react filtering
- Category
- JavaScript
- Title
- how to Write a program that simulates a coin toss using random method of Javascript Math class
- Category
- JavaScript
- Title
- how to delete an element from an array in javascript
- Category
- JavaScript
- Title
- how to make item not dragable in react-sortablejs
- Category
- JavaScript
- Title
- check if objects are equal javascript
- Category
- JavaScript
- Title
- color js
- Category
- JavaScript
- Title
- getcollectionnames
- Category
- JavaScript
- Title
- filter javascript array
- Category
- JavaScript
- Title
- how to convert string to int js
- Category
- JavaScript
- Title
- change select value jquery
- Category
- JavaScript
- Title
- export component in
- Category
- JavaScript
- Title
- after click text editior open in javascript
- Category
- JavaScript
- Title
- How to change htm h1 from nodejs
- Category
- JavaScript
- Title
- add to json object javascript
- Category
- JavaScript
- Title
- find the matching property
- Category
- JavaScript
- Title
- get today's date javascript
- Category
- JavaScript
- Title
- express render
- Category
- JavaScript
- Title
- how to mass comment in p5
- Category
- JavaScript
- Title
- alert user javascript
- Category
- JavaScript
- Title
- angular add debounce time before putting valu in subject next
- Category
- JavaScript
- Title
- ajax request qml
- Category
- JavaScript
- Title
- can we add new state property using setstate in react
- Category
- JavaScript
- Title
- array.unshift in javascript
- Category
- JavaScript
- Title
- accept 2 values after decimal in angular forms
- Category
- JavaScript
- Title
- button style not working react native
- Category
- JavaScript
- Title
- assign values to a table from javascript in VF page
- Category
- JavaScript
- Title
- array unique values javascript
- Category
- JavaScript
- Title
- change width in js
- Category
- JavaScript
- Title
- how to get value in array object value using for loop in javascript
- Category
- JavaScript
- Title
- array contains case insensitive javascript
- Category
- JavaScript
- Title
- deno
- Category
- JavaScript
- Title
- how to save thing in cookie js
- Category
- JavaScript
- Title
- initialize array javascript
- Category
- JavaScript
- Title
- express server replit
- Category
- JavaScript
- Title
- how to convert audio blocb to base64 string
- Category
- JavaScript
- Title
- example of validating fields on your own in express
- Category
- JavaScript
- Title
- change p tag text javascript
- Category
- JavaScript
- Title
- edit onclick event
- Category
- JavaScript
- Title
- javascript are arrays equal
- Category
- JavaScript
- Title
- colors in node js console
- Category
- JavaScript
- Title
- get url of page in background script
- Category
- JavaScript
- Title
- get day js
- Category
- JavaScript
- Title
- How to get the path to the file that required your module?
- Category
- JavaScript
- Title
- how to get element by title js
- Category
- JavaScript
- Title
- get value onChange from mat-select angular
- Category
- JavaScript
- Title
- how to update jquery datatable row cell value
- Category
- JavaScript
- Title
- How to create a function in javascript
- Category
- JavaScript
- Title
- how to animate array oy react elements
- Category
- JavaScript
- Title
- angular rebuild
- Category
- JavaScript
- Title
- if clicked outside of div jquery
- Category
- JavaScript
- Title
- how to make a inventory in js
- Category
- JavaScript
- Title
- append img to svg d3 js
- Category
- JavaScript
- Title
- check unique object in array javascript site:stackoverflow.com
- Category
- JavaScript
- Title
- dropzone add download button addedfile
- Category
- JavaScript
- Title
- Error: Can't resolve 'fs' webpack
- Category
- JavaScript
- Title
- how to import npm packages in node.js?
- Category
- JavaScript
- Title
- how to see if a web site is useing react
- Category
- JavaScript
- Title
- function to create an element javascript
- Category
- JavaScript
- Title
- create an element jquery
- Category
- JavaScript
- Title
- how to attach javascript to html
- Category
- JavaScript
- Title
- discord.js multiple embeds
- Category
- JavaScript
- Title
- chart.js radar chart
- Category
- JavaScript
- Title
- find last element with class jquery
- Category
- JavaScript
- Title
- async await javascript push
- Category
- JavaScript
- Title
- insert into mongodb node js
- Category
- JavaScript
- Title
- Javascript check if undefinded
- Category
- JavaScript
- Title
- how to push values in array
- Category
- JavaScript
- Title
- excel javascript api protect worksheet
- Category
- JavaScript
- Title
- how to make a div scrollable
- Category
- JavaScript
- Title
- discord.js send message to channel
- Category
- JavaScript
- Title
- how to appendChild in the begin of the div javascript
- Category
- JavaScript
- Title
- ionic react use yarn
- Category
- JavaScript
- Title
- how to parse json
- Category
- JavaScript
- Title
- how to hide javascript element by class
- Category
- JavaScript
- Title
- get string of element
- Category
- JavaScript
- Title
- firebase app named default already exists react native
- Category
- JavaScript