JSFiddle - React, Tailwind, and code Playground
JavaScript
(function() {
var ids = {};
function Vehicle() {
}
Vehicle.prototype.applyVars = function() {
var name = this.constructor.name,
id = ids[name];
if(!id) {
id = ids[name] = 0;
}
this.id = name + "-" + id;
ids[name]++;
};
function Car() {
this.applyVars();
}
function Truck() {
this.applyVars();
}
Car.prototype = new Vehicle;
Car.prototype.constructor = Car;
Truck.prototype = new Vehicle;
Truck.prototype.constructor = Truck;
window.Vehicle = Vehicle;
window.Car = Car;
window.Truck = Truck;
})();
var car1 = new Car,
car2 = new Car,
truck1 = new Truck;
alert([car1.id, car2.id, truck1.id]);