Creating Class in JavaScript | Simulating Classes Using Function in JavaScript | JavaScript Programmer Guide | JavaScript Tutorial

 JavaScript is a class-less language, however classes can be simulated using functions. The most common approach to achieving this is by defining a JavaScript function where we then create an object using the new keyword. this can be used to help define new properties and methods for the object as follows:

// A car 'class'
function Car(model) {
   this.model = model;
   this.color = 'silver';
   this.year = '2012';
   this.getInfo = function () {  
       return this.model + ' ' + this.year;
   }
}

We can then instantiate the object using the Car constructor we defined above like this:

var myCar = new Car('ford');
myCar.year = '2010';
console.log(myCar.getInfo());