To create an object, in principle, you can create it using the "constructor" function or simply with the object name "{}", which both do exactly the same thing in your case above.
The main advantage of a constructor template is that it will be more useful if you need to deal with arguments when creating an instance, as shown below.
function Car(model) { this.model = model; }
Or, if you need to use private variables and methods, as shown below.
function Car() { // private variable var modelYear = 1990; // private method var isEligibleToInsurance = function() { return this.modelYear > modelYear; }; this.isLatestModel = function() { return isEligibleToInsurance(); }; }
This way you can have a private method and use it inside public methods. When you use object notation, you cannot have a common private method used in all public methods, and when you use 'prototype', you cannot use private methods declared inside the constructor.
Note When you name the class, you can follow the pascal example to name it "Car" instead of "car" and create instances with a camel case as "var car = new Car ();"
source share