ECMAScript 6 Classes.

One of the main feature that introducing by the ES6 is Javascript Classes

ECMAScript 6 Classes.
by Salu S posted on Mon, Aug 01, 2016 in ES6

ECMAScript 6 (ES6, often referred to as Harmony) is the upcoming sixth major release of the ECMAScript language specification.The technology bringing a lot of amazing features that we will need to know. One of the main feature that introducing by the ES6 is “Javascript Classes”.

Classes

Classes are the main new OOP paradigm in ES6 which make code syntatic sugar.That means classes make prototype-based OO pattern much simpler and boosts interoperability.Classes supports inheritance,instance and static methods which makes ES6 more user friendly.

Class body and methods

The body of a class is enclosed in curly braces {}.Here we can define methods or constructors.

Constructor:

A special method for creating and initializing an object created with a class.

Example:

class Number{
    constructor(num1, num2) {
        this.num1 = num1;
        this.num2 = num2;
    }
    calcSum() {
        return this.num1 + this.num2;
    }
    getSum(){
        return this.calcSum();
    }
}
numOperation = new Number(20, 10);
console.log(numOperation.getSum);

Subclassing using extend keyword:

The extends keyword is used in class declarations or class expressions to create a class as a child of another class.

Example:

class Animal {
    constructor(name) {
        this.name = name;
    }
    speak() {
        console.log(this.name + ' makes a noise.');
    }
}
class Dog extends Animal {
    speak() {
        console.log(this.name + ' barks.');
    }
}
All product names, logos, and brands are property of their respective owners. All company, product and service names used in this website are for identification purposes only. Use of these names, logos, and brands does not imply endorsement.

Write a comment on ECMAScript 6 Classes.