JavaScript: the prototype design pattern

by gabrieleromanato

JavaScript

var vehiclePrototype = {

    init: function(carModel) {
        this.model = carModel;
    },

    getModel: function() {
        alert('The model of this vehicle is ' + this.model);
    }
};


function Vehicle(model) {

    function F() {};
    F.prototype = vehiclePrototype;

    var f = new F();

    f.init(model);
    return f;

}

var car = Vehicle('Ferrari');
car.getModel();