Skip to content

Instantly share code, notes, and snippets.

@st3v3nhunt
Created March 30, 2011 20:55
Show Gist options
  • Save st3v3nhunt/895283 to your computer and use it in GitHub Desktop.
Save st3v3nhunt/895283 to your computer and use it in GitHub Desktop.
OO JavaScript
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