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

Array in ES6

Array in ES6 The array can be defined as an object that is used to display the set of homogenous type of elements in ECMAScript 6. The array provides the facility to store...

5 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.

Cookies in ES6

Cookies in ES6 The cookies are an old client-side storage technique produced in a server-side scripting language such as Php and ASP etc. The cookie can be defined as a small...

6 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.

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.

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.

ES6 | ECMAScript 6 Syntax

The term 'syntax' defines the grammar and spelling of any programming language. In Computer science, “Term syntax is a collection of some rules and regulations that determines the group of symbols used in...

8 minutes read.

Dialog box in ES6

Dialog box in ES6 A dialog box can be defined as a regular type of window in the Graphical User Interface (GUI) of the operating system. It is also spelled as...

5 minutes read.

Array Methods in ES6

Array Methods in ES 6 ES 6 introduced the various new methods such as Array,from(), Array,of(). Here, we have the various array methods used in JavaScript, they are given below in the table...

10 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.

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.

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.

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.

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.

Types of Inheritance in ES6

Types of Inheritance The term inheritance can be divided into following three parts- Single-level InheritanceMultiple InheritanceMulti-level Inheritance Single-level Inheritance The single-level inheritance can be defined as an inheritance in which the child class can...

5 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.

Generator in ES6 | ECMAScript 6

ECMAScript 6 recommends a new way of working with iterators and functions. The way is named as generator or generator function. The ES 6 generator is a distinct type of function that...

5 minutes read.

Page Redirect in ES6

Page Redirect in ES6 The term redirect can be defined as a process to send the user and search engine on a specified URL from the original page. “The page redirection is...

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.