Class in JavaScript

In JavaScript class is defined with the constructor.

Class declaration:

class circle{

   constructor(r){

   this.r;

   }

}

First declare class otherwise give an reference error, because class is not housing but method is housing.

Reference error:

const p =new Rectangle();

class Rectangle{

…..

}

Ex.

class Rectangle {

    constructor(height, width){

    this.height=height;

    this.width=width;

 }

get area(){

    return this.calcArea();

 }

calcArea() {

    return this.height * this.width;

 }

}

const square = new Rectangle(10,10);

const square = new Rectangle(10,10);

Powered  by Codersarts