ECMAScript 6 Classes.

Sun Jul 31 2016

ECMAScript 6 Classes.

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.')
  }
}
Leave a comment

To make a comment, please send an e-mail using the button below. Your e-mail address won't be shared and will be deleted from our records after the comment is published. If you don't want your real name to be credited alongside your comment, please specify the name you would like to use. If you would like your name to link to a specific URL, please share that as well. Thank you.

Comment via email
Salu S
Salu S

JavaScript Developer at Lightrains Tech

Tags Recent Blogs