Creating objects
We have seen in previous chapters the different ways JavaScript allows us to create objects. We can use the literal notation to create objects in an extremely easy manner or we can apply a constructor function, we can create instances of a class
definition or invoke the Object.create()
method. Each approach has its benefits and drawbacks, and we can use what we consider most suitable for our needs.
The literal notation is the easiest approach. We can just create an object by defining its properties and methods between curly braces:
var johnSmith = { name: "John", surname: "Smith"};
The positive side of this approach is its simplicity. The negative side is that we need to specify each property and method and it is not suitable to create many similar objects. Usually, its use is limited to the creation of a single object.
The creation of objects based on constructor functions or classes allows us to define an object template from which we can create as many objects as we want...