Last active
August 29, 2015 14:15
-
-
Save todiadiyatmo/306c7e9130f1063f3be0 to your computer and use it in GitHub Desktop.
Javascript OOP - Prototype Boilerplate
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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