Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

super in javascirpt

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';
  }
}
Comment

javascript super

class Parent {
  constructor() {}
  method() {}
}
class Child extends Parent {
  constructor() {
    super() // Parent.constructor
    super.method() // Parent.method
  }
}
Comment

JavaScript super() keyword

// 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();
Comment

super method in js

//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())
Comment

Super in javascript

super([arguments]); // calls the parent constructor.
super.functionOnParent([arguments]);
Comment

super keyword in javascript

<!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>
Comment

PREVIOUS NEXT
Code Example
Javascript :: react why onclick property function trigger without click 
Javascript :: material ui icons next js 
Javascript :: javascript scroll 
Javascript :: accordion reatjs 
Javascript :: d3 paning 
Javascript :: jquery repeater 
Javascript :: react recursive component 
Javascript :: adding debounce in autocomplete material ui 
Javascript :: vue compare two dates 
Javascript :: filter in javascript 
Javascript :: line break in js 
Javascript :: Select HTML elements by CSS selectors 
Javascript :: learn nodejs 
Javascript :: modulus js 
Javascript :: Modal Dialogs in React 
Javascript :: vuejs show content on loaded 
Javascript :: react native expo flatlist 
Javascript :: multi-dimensional array js 
Javascript :: rest api full form 
Javascript ::  
Javascript :: grid in chart.js 
Javascript :: context api 
Javascript :: how to use buffer in angular by using browserify 
Javascript :: javascript date array 
Javascript :: react change background image on hover 
Javascript :: how to create a object in javascript 
Javascript :: date range query knex 
Javascript :: 1 day ago javascript 
Javascript :: new date 
Javascript :: Javascript using forEach loop to loop through an array 
ADD CONTENT
Topic
Content
Source link
Name
6+6 =