Creating objects

There are two ways of creating an object in JavaScript: using the object literal, or using a constructor. The object literal is used when we want to create fixed objects, whereas a constructor is used when we want to create objects dynamically at runtime.

Let's consider a case where we may need to use the constructor instead of the object literal. Here is a code example:

const student = {      name: "Eden",      printName() {          console.log(this.name);      } } student.printName(); //Output "Eden"

Here, we created a student object using the object literal, that is, the {} notation. This works well when you just want to create a single student object.

But the problem arises when you want to create multiple student objects. Obviously, you ...

Get Learn ECMAScript - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.