class Rectangle {
constructor(height, width) {
this.name = 'Rectangle';
this.height = height;
this.width = width;
}
sayName() {
console.log('Hi, I am a ', this.name + '.');
}
get area() {
return this.height * this.width;
}
set area(value) {
this._area = value;
}
}
class Square extends Rectangle {
constructor(length) {
this.height; // ReferenceError, super needs to be called first!
// Here, it calls the parent class's constructor with lengths
// provided for the Rectangle's width and height
super(length, length);
// Note: In derived classes, super() must be called before you
// can use 'this'. Leaving this out will cause a reference error.
this.name = 'Square';
}
}
class Parent {
constructor() {}
method() {}
}
class Child extends Parent {
constructor() {
super() // Parent.constructor
super.method() // Parent.method
}
}
// parent class
class Person {
constructor(name) {
this.name = name;
}
greet() {
console.log(`Hello ${this.name}`);
}
}
// inheriting parent class
class Student extends Person {
constructor(name) {
console.log("Creating student class");
// call the super class constructor and pass in the name parameter
super(name);
}
}
let student1 = new Student('Jack');
student1.greet();
//class in es6 are just functional constructor.
//Parent class is Person and Developer class inheritant from Person class using
//extend and super method
class Person{
constructor(firstname,lastname){
this.firstname= firstname;
this.lastname=lastname;
}
aboutPerson(){
console.log(`My name is ${this.firstname} ${this.lastname} `)
}
}
class Developer extends Person{
constructor(firstname,lastname,experience,projects){
/* //super keyword is used to call the constructor
of its parent class to access the parent's properties and methods*/
super(firstname,lastname);
this.experience=experience;
this.projects=projects;
aboutDev(){
console.log(`My name is ${this.firstname} and I have ${this.experience}
in software development`)
}
const ShirshakDev= new Developer('Shirshak','Kandel',3,13)
console.log(ShirshakDev.aboutDev())
super([arguments]); // calls the parent constructor.
super.functionOnParent([arguments]);
<!DOCTYPE html>
<html>
<head> </head>
<body>
<script>
class Person {
constructor(name, age) {
this.name = name;
this.age = age;
}
atWork() {
return this.name + " is at work, ";
}
atHome() {
return this.name + " is at home";
}
sleeping() {
return this.name + " is sleeping";
}
}
class FashionDesigner extends Person {
constructor(name, age) {
super(name, age);
}
profession() {
return this.name +
" is a Fashion Designer";
}
doTasks() {
return super.atWork() + this.profession();
}
}
function display(content) {
console.log(content);
}
const character =
new FashionDesigner("Sayan", 30);
display(character.profession());
display(character.atHome());
display(character.doTasks());
</script>
</body>
</html>