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 :: how to combine two regular expressions in javascript 
Javascript :: listen for double click before click 
Javascript :: express project structure 
Javascript :: form status angular 
Javascript :: angular json and cli json file 
Javascript :: nestjs framwork 
Javascript :: javascript for loop 
Javascript :: useformik checkbox multiselect 
Javascript :: schema in mongoose 
Javascript :: define methods of objects in javascript 
Javascript :: print js example 
Javascript :: Delete - Cloudinary 
Javascript :: convert image url to base64 javascript without canvas 
Javascript :: react useeffect hooks 
Javascript :: regular expression for beginners javascript 
Javascript :: react render children inside parent component 
Javascript :: github create react app buildpack 
Javascript :: Destructuring array and object from a nested object 
Javascript :: create object from number 
Javascript :: momoent isafter or equal$ 
Javascript :: generate uuid 
Javascript :: puppeteer block request javascript 
Javascript :: lodash sum 
Javascript :: javascript nested loop 
Javascript :: js export options 
Javascript :: jquery is not defined error in wordpress 
Javascript :: delete method 
Javascript :: closure in javascript 
Javascript :: regex forms 
Javascript :: js regex return null 
ADD CONTENT
Topic
Content
Source link
Name
8+2 =