Class in ES6

Class in ES6: The Class is a basic term in object-oriented programming (OOPs). A class helps to determine the prototype for modeling in real-world objects. It is used to formulate the program code into logical parts and make it reusable.

The definition of a class only contains the functions and constructors.

A class consists of the constructor used to allot the memory blocks to the class objects.   

A class also involves the function used to perform the operations to the objects.

Syntax of class expression

var variable_name = new Class_name{
 }

Class Declaration

The class declaration is referred to declare the class name with class attributes such as the parent class of the class. The declaration process must contain the class keyword.

Syntax of class declaration

class Class_name {
}

Here, we have an example to understand the class expression and class declaration.

Example 1

var Employee = class                // Class Expression
{
          constructor (name, age)
{
          this.name = name;
          this.age = age;
}
}

Example 2

class Employee                                 // Class Declaration
{
          constructor (name, age)
{
          this.name = name;
          this.age = age;
}
}

Important Point: The class body contains the methods instead of data properties.

Object creation from the class

We can easily create or install an object from a class with the help of new keyword.

Syntax

var object_name =new class_nam([arguments])    

Example

var emp = new employee (‘Jackson’, 25)

Function Accessing

We can access the functions and attributes of a class with the help of an object. The notation dot (‘.’) is used to access the class data members.

Syntax

Object function_name();

Example

Here, we have an example to illustrate the concept of function accessing.

‘use strict’
class Employee
{
          constructor (name, age)
{
          this.name = name;
          this.age = age;
}
}
Emp()
{
          console.log (“The Employee name is: ”, this.name)
          console.log (“The age of Employee is: ”, this.age)
}       
var EmpObj = new Employee (‘Robin’, 23);
EmpObj.Emp;

Output

After the execution of the above code, we got the following output:

Class in ES6

Important Point: The constructor definition is necessary for the class because, by default, every class contains constructor.

The Static Keyword

The Static keyword is used to create a static function inside the class. The static functions are always declared with the name of the class.

Example

‘use strict’
class Sample
{
          static show ()
{
          Console.log (“Static Function”);
}
}
Sample.show()                             // call the static method

Output

After the execution of the above example, we got the following output:

Class in ES6

The Class Inheritance

ES6 introduced the class inheritance using super and extends keywords. Before the ECMAScript 5, the execution of class inheritance took various steps. The ES6 clarifies the concept of class inheritance.

The term inheritance can be defined as an object’s ability to access the methods and other properties of other objects. In JavaScript, the inheritance works by using prototypes. The particular form of inheritance is known as prototypal inheritance. The class, we extended to create another class is called Parent/Superclass and the newly created class is called Child/Sub class.

We can easily inherit a class from another class with the help of extends keyword. The child class can easily inherit the properties from the parent class except the constructor.

Syntax

class sub_class_name extendes super_class_name {
}

We can easily inherit a class from another class with the help of extends keyword.

Example

‘use strict’
class Employee
{
          constructor (p)
{
          this.name = p;
}
}
class User extends Employee
{
          show()
{
          console.log (“The name of employee is:” +this.name)
}
}          
var object = new User(‘Robinson);
object.show ()

Output

After the execution of the code, we got the following output:

Class in ES6


Related Topics

Operators in ES6 | ECMAScript 6

Operators in ECMAScript 6 The term Operator is refers to a symbol that is used to instruct the computer system to perform a specific operation. The ECMAScript 2015 supports a rich set of operators....

11 minutes read.

Environment Set up in ES6

Environment Set up in ES6 JavaScript is a portable language. It can execute on any browser or any host, and any operating system. Before the use of JavaScript, we need to...

5 minutes read.

Loops in ES6 | ECMAScript 6

Loops in ECMAScript 6 A Loop statement can be defined as a group of instructions that are always repeated. In the computer programming language, the loops help us to execute a collection of instructions...

8 minutes read.

Object Destructuring in ES6

Object Destructuring in ES6 The term destructuring is referred to as the breakdown of the complex entities into simpler parts. It works like array destructuring. In the array destructuring, the values...

3 minutes read.

Validation in ES6

Validation in ES6 The term validation can be defined as a mechanism to check the information given by the user is correct or not according to the requirements. If the data...

9 minutes read.

Animation in ES6

Animation in ES6 In JavaScript, the animation feature is used to manage the aspect that the CSS (Cascading Style Sheet) can’t. JavaScript provides various new elements that help us to construct...

8 minutes read.

Spread Operator in ES6

Spread Operator in ECMAScript 6 The term 'Spread Operator' can be defined as an operator that allows expending the array into separate elements. Three dots (…) isthe syntax of the Spread Operator.The Spread operator...

5 minutes read.

Set in ES6

Set in ES6 A Set is a kind of object used to create a group of unique values. The values stored in a set can be string, literal, array, and integer....

6 minutes read.

Number in ES6

Number in ES6 In ES6, we have various methods and properties to implement numeric functions such as floating-point, integers, date, etc. The numbers in ES6 enable us to work with number...

7 minutes read.

Variables in ES6 | ECMAScript 6

Variables in ECMAScript 6 A Variable is a place in memory used to store a value. There are some rules for declaration of the variable- Variable Initialization Initialization is a process of locating and storing the initial...

4 minutes read.

Functions in ES6 | ECMAScript 6

Functions in ECMAScript 6 In ECMAScript programming language, the term ‘function’ can be defined as a collection or group of input statements, used to perform particular computational calculations and generate the output...

9 minutes read.

ES6 Tutorial

Introduction of ES6 The ECMAScript 6 is a scripting language specification institutionalized by ECMA International. ECMA, named in 1994, is an international organization, related to information and communication system. ECMA stands for “European Computer Manufacturers...

8 minutes read.

Events in ES6

Events in ES6 The events can be defined as the actions or the occurrence of an action that is perceived by the software. The event is used to handle the interaction between...

14 minutes read.

Array Destructuring in ES6

Array Destructuring in ES6 The term ‘Destructuring’ is defined as dividing the complicated data structure into easier modules. The Destructuring syntax helps to extract the small segments from an array and the objects....

3 minutes read.

Arrow Function in ES6

Arrow Function in ES6  An Arrow function is a new concept recommended in ECMAScript 2015. It is also called “Fat Arrow Function.” It helps us to write the function more accurately. This function...

5 minutes read.

Template Literals in ES6

Template Literals in ES6            ES6 introduced a new feature called template literals. It facilitates us to define a multiline string and executes the string interpolation. The template literals are referred to...

3 minutes read.

Promises in ES6

Promises in ES6 A promise is used to represent some aspects that are finally fulfilled. It can also be resolved or discarded according to the operational output. In ES6, a promise...

8 minutes read.

Object in ES6

Object in ES6 The term object can be defined as an instance that contains the group of key-value pairs. We can modify it using the life cycle of an object in...

6 minutes read.

Boolean in ES6

Boolean in ES6 In ES6, the Boolean object is used to show two values either it may be true or false. In JavaScript, we can use the Boolean object as a...

4 minutes read.

Loop Control Statement in ES6

Loop Control Statement in ECMAScript 6 The loop control statements can be defined as a set of conditions that control the execution of the loop. It executes the loop until the condition becomes false....

6 minutes read.