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 :: word table to json 
Javascript :: What are "res" and "req" parameters in Express functions 
Javascript :: check the type of a variable in js 
Javascript :: javascript stack 
Javascript :: lazy loading by scroll vue 
Javascript :: firebase integration in react 
Javascript :: what does onchange do in react 
Javascript :: scss variables in react 
Javascript :: remove duplicates strig javascript 
Javascript :: fetch composition API in Vue3 
Javascript :: work with query string javascript 
Javascript :: quote 
Javascript :: cookie-parser get cookie 
Javascript :: javascript break out of map 
Javascript :: resize window javascript 
Javascript :: javascript making a tag game 
Javascript :: JavaScript ForEach This Argument 
Javascript :: react facebook login 
Javascript :: javasript object 
Javascript :: array count in mongoose query 
Javascript :: comments in jsx 
Javascript :: destructuring an array 
Javascript :: npm react animation 
Javascript :: Geometery parsing GeoJSON 
Javascript :: js 2d array includes 
Javascript :: flatten array 
Javascript :: validate decimal number with 6 decimal digits javascript 
Javascript :: functions and variables javascript 
Javascript :: React passing data fom child to parent component 
Javascript :: how to usestate in react 
ADD CONTENT
Topic
Content
Source link
Name
8+6 =