classs methods js code example

Example 1: es6 class example

'use strict' 
class Polygon { 
   constructor(height, width) { 
      this.h = height; 
      this.w = width;
   } 
   test() { 
      console.log("The height of the polygon: ", this.h) 
      console.log("The width of the polygon: ",this. w) 
   } 
} 

//creating an instance  
var polyObj = new Polygon(10,20); 
polyObj.test();

Example 2: how to create instance of class in javascript

class Hello {
	person(name){
    	this.name = name;
    }
  	// Constructor - Important except for JSX
    constructor(name){
    	this.person(name);
        console.log(`Hello ${this.name}`);
    }
}

const helloInstance = new Hello('john doe');