- In ECMA 6, class can be created using the keyword class
- In earlier version like ECMA 5, calling a function using the new operator returns an object. So you can say , “when we call a function using the new operator (which is also known as ‘constructor invocation pattern’), function acts like a class and returns a newly created object ”
Class in ECMA 6
In ECMA 6, you can create a Student class as shown in the listing below:
ECMA 6 allows you to create a class using the keyword “class”. You can create object of Student class using the new operator as shown in the listing below:
Like function declaration and expression, class can also be created
- Class declaration
- Class expression
Above, you created Student class using the class declaration syntax. Same Student class can be created using the class expression syntax as shown in the listing below:
Above, you created Student class using the class unnamed expression syntax. Same Student class can be created using named class named expressions syntax as shown in the listing below:
Keep in mind that name given to class expression is local to the class body.
Hoisting of the class
Did you remember that functions declaration is hoisted? Means, you can call a function declaration before it is created.
On running above code will throw you ReferenceError exception.
A class can be referred in the function which comes before class declaration. However, you can call such function only after the declaration of the class. Let us consider below code
In above code, first function call will throw reference error, whereas second function call will work fine.
Methods in class
Most often you will create these three types of methods in the class,
A constructor method is a special method. Constructor method creates and initializes the object created with the class. A class can have only one constructor method. More than one constructor method in the class will throw exception. A class with constructor can be created as shown in the listing below:
You can call static method with the class name as shown below:
These prototype methods can be used with instance of the class as shown in the listing below:
These prototype properties are actually property of the Student.prototype.
Output of above code would be function.
Class in ECMA 5
You do not create class using the class keyword in ECMA 5. If you wish to create same class Student in ECMA 5, create a function with the name Student as shown in the listing below:
You can add prototype methods as shown in the listing below:
Now to create objects, call function Student using the new operator as shown in the listing below:
Calling a function using the new operator is also called as “Constructor Invocation Pattern”. Therefore, in ECMA 5, when you call a function using the new operator, function acts as a class and returns an object.
Here, you also learnt what constructor invocation pattern is. I hope now you should able to answer
- How to create class in ECMA 6
- How to create class in ECMA 5
- What is Constructor Invocation Pattern
I hope it helps. Thanks for reading.