ECMAScript 6.0

Class in ECMAScript 6.0

As JavaScript is a prototype based object oriented language so we were require to do simulation for creating class in javascript. But ECMAScript 6 defines classes in actual by introducing the ‘class’ keyword.Before ECMAScript 6.0 how to create a class in javascript we have seen in:-
https://smartfrontend.wordpress.com/2016/05/29/creating-class-in-javascript/

But with ECMAScript 6.0 there is clean way for defining the class in javascript.Writting same class as in above mentioned link in ECMAScript 6.0.Below is code for creating class with name ‘Company’:-


class Company {
	//Define Constructor of Class.
    constructor(name,yearOfFoundation,noOfEmployee){
        this.name = name;
        this.yearOfFoundation = yearOfFoundation;
        this.noOfEmployee = noOfEmployee;
    }

    getCompanyInfo() {
        return this.name + ' ' + this.yearOfFoundation + ' '+ this.noOfEmployee;
    }
}

We can create the object of this class as :-


  //Creating Instance of Company.
  var company = new Company('Comp1','1997','1000');

  //calling method of instance.
  var str = company.getCompanyInfo();

Hope this will be helpful for someone.