Skip to content

Instantly share code, notes, and snippets.

@goldhand
Created August 17, 2015 00:46
Show Gist options
  • Save goldhand/abdcdb2dc1ff0ca36991 to your computer and use it in GitHub Desktop.
Save goldhand/abdcdb2dc1ff0ca36991 to your computer and use it in GitHub Desktop.
function Car() { // constructor
this.driverCount = 0; // this refers to the newly created object
}
Car.prototype.incrementDriverCount = function() {
// increments the driverCount of the instance of the new object invoking this method
this.driverCount++; // this still refers to the object who is invoking this function
return this.driverCount;
};
var myCar = new Car();
var myOtherCar = new Car();
var myWeekendCar = new Car();
@goldhand
Copy link
Author

function increaseCarDriverCount() {
  this.driverCount++;
  return this.driverCount;
}

function Car() {
  this.driverCount = 0;
  this.incrementDriverCount = increaseCarDriverCount;
}

var myCar = new Car();
var myOtherCar = new Car();
var myWeekendCar = new Car();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment