Created
March 30, 2011 20:55
-
-
Save st3v3nhunt/895283 to your computer and use it in GitHub Desktop.
OO JavaScript
This file contains 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
function Car (doors, fuel, color) { | |
this.doors = doors; | |
this.color = color; | |
this.fuel = fuel; | |
this.changeFuel = function () { | |
if (this.fuel === 'petrol') { | |
this.fuel = 'diesel'; | |
} else { | |
this.fuel = 'petrol'; | |
} | |
} | |
} | |
var fiesta = new Car(5, 'petrol', 'red'); | |
var skyline = new Car(4, 'petrol', 'black'); | |
fiesta.doors === 5; | |
skyline.doors === 4; | |
skyline.changeFuel(); | |
skyline.fuel === 'diesel'; // true | |
// prototype can be used to add additional methods to all instances of Car | |
Car.prototype.changeColor = function (color) { this.color = color }; | |
fiesta.changeColor('white'); | |
fiesta.color === 'white'; // true |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment