JavaScript Interview Day # 1 - How To Create A Class

Have you ever been asked,  “How do you create class in JavaScript?” I am sure, regardless of the level of the JavaScript interview, you must have come across this question. Well, there is not one answer to this question. However, before you go ahead and read detailed answer in this post, let me give you a gist in two bullet points.

  1. In ECMA 6, class can be created using the keyword class.
  2. 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.

  1. class Student {  
  2.   
  3.     constructor(name, age) {  
  4.         this.name = name;  
  5.         this.age = age;  
  6.     }  
  7.   
  8.     getData() {  
  9.         console.log(this.name + " is " + this.age + " years old !");  
  10.     }  
  11.   
  12. }   

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.

  1. var s1 = new Student("foo", 7);  
  2. var s2 = new Student("bar", 9);  
  3. s1.getData();  
  4. s2.getData();   

Like function declaration and expression, class can also be created.

  1. Class declaration
  2. Class expression

    JavaScript

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.

  1. var Student = class {  
  2.   
  3.     constructor(name, age) {  
  4.         this.name = name;  
  5.         this.age = age;  
  6.     }  
  7.   
  8.     getData() {  
  9.         console.log(this.name + " is " + this.age + " years old !");  
  10.     }  
  11.   
  12.     toString() {  
  13.         return `(${this.name},${this.age})`;  
  14.     }  
  15.   
  16. }   

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.

  1. var Student = class s {  
  2.   
  3.     constructor(name, age) {  
  4.         this.name = name;  
  5.         this.age = age;  
  6.     }  
  7.   
  8.     getData() {  
  9.         console.log(this.name + " is " + this.age + " years old !");  
  10.     }  
  11.   
  12.     toString() {  
  13.         return `(${this.name},${this.age})`;  
  14.     }  
  15.   
  16. }   

Keep in mind that the name given to class expression is local to the class body.

Hoisting of the class

Did you remember that functions declaration is hoisted? This means, you can call a function declaration before it is created.

  1. foo();  
  2.   
  3. function foo() {  
  4.     console.log("I am hoisted");  
  5. }   

In the above listing, you declared a function and that is hoisted, so you are able to execute it before it is created. However, classes are not hoisted in JavaScript. Does not matter whether you are creating a class as declaration or expressions, they are not hoisted. So, if you do something, as shown in below listing.

  1. var s1 = new Student("foo", 7);  
  2. var s2 = new Student("bar", 9);  
  3.   
  4. class Student {  
  5.   
  6.     constructor(name, age) {  
  7.         this.name = name;  
  8.         this.age = age;  
  9.     }  
  10.   
  11. }   

Running the above code will throw you ReferenceError exception.

JavaScript

Therefore, the conclusion is that it  is unlikely JavaScript functions classes are not hoisted.

JavaScript

A class can be referred to in the function which comes before class declaration. However, you can call such function only after the declaration of the class. Let us consider the below code.

  1. function printstudent() {  
  2.   
  3.     var s1 = new Student("foo", 7);  
  4.     console.log(s1.name);  
  5.   
  6. }  
  7.   
  8. printstudent(); // Student is not defined error   
  9.   
  10. class Student {  
  11.   
  12.     constructor(name, age) {  
  13.         this.name = name;  
  14.         this.age = age;  
  15.     }  
  16.   
  17. }  
  18. printstudent(); // print foo   

In the 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.

  1. Constructor
  2. Static
  3. Prototype

Constructor method

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 methods in the class will throw exception. A class with constructor can be created as shown in the listing below.

  1. class Student {  
  2.   
  3.     constructor(name, age) {  
  4.         this.name = name;  
  5.         this.age = age;  
  6.     }  
  7.   
  8. }  

 JavaScript

Static methods

Like any other programming languages, JavaScript static methods are called without creating the object of the class. You can create a class with static method as shown in the listing below, 

  1. class Student {  
  2.   
  3.     static LogStudent() {  
  4.   
  5.         return "I am static method";  
  6.   
  7.     }  
  8.   
  9. }   

You can call static method with the class name, as shown below. 

  1. var n = Student.LogStudent();  
  2. console.log(n);   

When you try calling static method with instance of the class, JavaScript will throw an exception. As of now JavaScript does not support static members or properties.

JavaScript

Prototype methods

JavaScript class prototype methods are the methods used by the instances of the class. They are the normal methods, which can be inherited and used by the objects. 

  1. class Student {  
  2.   
  3.     constructor(name, age) {  
  4.         this.name = name;  
  5.         this.age = age;  
  6.     }  
  7.   
  8.     getData() {  
  9.         return this.name + " is " + this.age + " years old !";  
  10.     }  
  11.   
  12. }   

These prototype methods can be used with instance of the class, as shown in the listing below.

  1. var s1 = new Student('foo', 9);  
  2. console.log(s1.getData());  
  3.   
  4. var s2 = new Student('bar', 7);  
  5. console.log(s2.getData());  

These prototype properties are actually property of the Student.prototype.

JavaScript

In JavaScript, functions have prototype property, and class has prototype property. Because type of class is function. So when you execute the below code- 

  1. class Student {  
  2.   
  3.     constructor(name, age) {  
  4.         this.name = name;  
  5.         this.age = age;  
  6.     }  
  7.   
  8.     getData() {  
  9.         return this.name + " is " + this.age + " years old !";  
  10.     }  
  11.   
  12. }  
  13.   
  14. console.log(typeof (Student));  

JavaScript

Output of the 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, 

  1. function Student(name, age) {  
  2.     this.name = name;  
  3.     this.age = age;  
  4. }   

You can add prototype methods as shown in the listing below, 

  1. Student.prototype.getData = function () {  
  2.     return this.name + " is " + this.age + " years old !";  
  3. }   

Now, to create objects, call function Student using the new operator as shown in the listing below, 

  1. var s1 = new Student("foo", 8);  
  2. console.log(s1.getData());  
  3.   
  4. var s2 = new Student("bar", 9);  
  5. console.log(s2.getData());   

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 learned what constructor invocation pattern is. I hope now you should able to answer,

  1. How to create class in ECMA 6
  2. How to create class in ECMA 5
  3. What is Constructor Invocation Pattern

I hope it helps. Thanks for reading.

X

Build smarter apps with Machine Learning, Bots, Cognitive Services - Start free.

Start Learning Now