Skip to content

Instantly share code, notes, and snippets.

@devdays
Created December 6, 2014 01:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save devdays/f27f76b89faac81e1b29 to your computer and use it in GitHub Desktop.
Save devdays/f27f76b89faac81e1b29 to your computer and use it in GitHub Desktop.
JavaScript prototypes example
function GreatDane() { }
var rover = new GreatDane();
var spot = new GreatDane();
GreatDane.prototype.getBreed = function() {
return "Great Dane";
};
// Works, even though at this point
// rover and spot are already created.
alert(rover.getBreed());
// this hides getBreed() in GreatDane.prototype
spot.getBreed = function() {
return "Little Great Dane";
};
alert(spot.getBreed());
// but of course, the change to getBreed
// doesn’t propagate back to GreatDane.prototype
// and other objects inheriting from it,
// it only happens in the spot object
alert(rover.getBreed());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment