Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

javascript abstract class

//there is no way to mark a class as abstract in ES6,
//however you can force a class to behave line one by
//	- forcing derived classes to override a method
//	- causing the base class's contructor to throw an error so that it
//			is never used to create instances of the base type*
// *Be careful, as this will cause problems if you do need derived classes
// 	to call super() contructor
class Foo {
 
    constructor(text){
       this._text = text;
    }
 
    /**
     * Implementation optional
     */
    genericMethod() {
        console.log('running from super class. Text: '+this._text);
    }
    
    /**
     * Implementation required
     */
    doSomething() {
       throw new Error('You have to implement the method doSomething!');
    }
 
}
 
class Bar extends Foo {
 
    constructor(text){
       super(text);
    }
 
    genericMethod() {
        console.log('running from extended class. Text: '+this._text);
    }
    
    doSomething() {
       console.log('Method implemented successfully!');
    }
    
}
 
let b = new Bar('Howdy!');
b.genericMethod(); //gonna print: running from extended class. Text: Howdy
b.doSomething(); //gonna print: Method implemented successfully!
Comment

Abstract class in js

class Employee
{
  constructor() {
    if(this.constructor == Employee){
    throw new Error(" Object of Abstract Class cannot be created");
    }
  }
  display() {
    	throw new Error("Abstract Method has no implementation");
    }
  }
  class Manager extends Employee
  {
    display(){
    //super.display();
    console.log("I am a Manager");
  }
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: ajax loader 
Javascript :: use css child selector inside js 
Javascript :: run for loop every second js 
Javascript :: delete duplicate array javascript 
Javascript :: md5 checksum javascript 
Javascript :: Modal Dialogs in React 
Javascript :: how to create two dimensional array in js 
Javascript :: react materialize cdn 
Javascript :: chrome.runtime.sendMessage 
Javascript :: javascript single thread 
Javascript :: js round 2 decimals 
Javascript :: copy object with new property in js 
Javascript :: onclick multiple functions react 
Javascript :: how to cause a whole page reload react redux 
Javascript :: how to copy a javascript array 
Javascript :: monaco editor get content 
Javascript :: passing event handler to useEffeect 
Javascript :: how to make an array of a value from 1 to the number 
Javascript :: sequelize contains 
Javascript :: javascript get clock time in auto counter up 
Javascript :: react propthpes or 
Javascript :: javascript color green to red 
Javascript :: pwa in angular 
Javascript :: open ai gym 
Javascript :: javascript iterate through an object 
Javascript :: dispay react component after some time 
Javascript :: include hover in style jsx 
Javascript :: remove object from array javascript 
Javascript :: working of timers in javascript 
Javascript :: Next js Linking example 
ADD CONTENT
Topic
Content
Source link
Name
2+7 =