Object Prototype

Defining a method prototype

by isogunro

JavaScript

//If 'var' was used, the variables would be private, preventing the use of prototypes.
function Vehicle(theYear, theMake, theModel) {
     this.year = theYear;
     this.make = theMake;
     this.model = theModel;
}
Vehicle.prototype.getInfo = function() {
    return 'Car: '+this.year+ ' '+this.make + ' '+this.model;   
}

var car1 = new Vehicle(1979, "Ford", "Mustang");
alert(car1.getInfo());

var van = new Vehicle(2004, "Chrysler", "Town and Country");
alert(van.getInfo());