Creating a class

by Sukanya Halder

HTML

<!-- 
Notes : 
There is no class keyword in JavaScript, but you can simulate a class by starting with a function, which is actually the constructor function of the object

There are several problems with this code. All the variables are defined without the var keyword, so year, make, model, and getInfo are automatically defined in the global scope and are accessible from anywhere.

The Vehicle function accepts three parameters and doesn’t return anything. Instead, it is setting global variables, and there is no provision for multiple instances.

The problem is that year, make, and model of the second
vehicle replaced year, make, and model of the first vehicle.

there is no encapsulation
-->

JavaScript

var car;
function Vehicle(year,modelname){    
    makemodel = modelname;
    makeyear = year;
    show= function(){
        alert(makemodel + "--"+makeyear);
    }
}
car = Vehicle("BMW","2015");
car2 = Vehicle("Mercedes","2015");
//car2.show = function(){alert("hello");}

//car.show();
//car2.show();
//Vehicle("BMW","2015");
show();