OOP - Javascript
Thanks to http://javascriptissexy.com/oop-in-javascript-what-you-need-to-know/
JavaScript
function Vehicle(options) {
options = options || {};
this.wheels = options.wheels || 4,
this.type = 'vehicle';
this.price = options.price || 0;
}
Vehicle.prototype = {
constructor: Vehicle,
set: function(property, value){
this[property] = value;
},
get: function(property) {
this.run();
return this[property];
},
run: function(){
alert('running by vehicle class');
}
};
function Car(options){
options = options || {};
Vehicle.call(this, options);
this.type = 'car';
};
Car.prototype = new Vehicle;
Car.prototype.run = function() {
alert('running by car class');
};
/* Testing */
console.log('');
console.log('var myVehicle = new Vehicle();');
var myVehicle = new Vehicle();
console.log(myVehicle);
console.log('');
console.log('var yourVehicle = new Vehicle({wheels: 12, price: 1});');
var yourVehicle = new Vehicle({wheels: 12, price: 1});
console.log(yourVehicle);
console.log('');
//yourVehicle.get('wheels');
console.log('var myCar = new Car({wheels: 5, price: 600});');
var myCar = new Car({wheels: 5, price: 600});
console.log(myCar);
console.log('');
myCar.get('wheels');