Skip to content

Instantly share code, notes, and snippets.

@todiadiyatmo
Last active August 29, 2015 14:15
Show Gist options
  • Save todiadiyatmo/306c7e9130f1063f3be0 to your computer and use it in GitHub Desktop.
Save todiadiyatmo/306c7e9130f1063f3be0 to your computer and use it in GitHub Desktop.
Javascript OOP - Prototype Boilerplate
var vehicle = {};
vehicle.drive = function () {
console.log('vrooom...');
};
// Create -> Make car "inherit" vehicle
var car = Object.create(vehicle);
car.honk = function() {
console.log('honk honk');
};
var myVehicle = Object.create(vehicle);
var myCar1 = Object.create(car);
var myCar2 = Object.create(car);
myCar1.honk(); // outputs "honk honk"
myCar2.honk(); // outputs "honk honk"
myVehicle.drive(); // outputs "vrooom..."
myCar1.drive(); // outputs "vrooom..."
myCar2.drive(); // outputs "vrooom..."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment