Created
December 6, 2014 01:40
-
-
Save devdays/f27f76b89faac81e1b29 to your computer and use it in GitHub Desktop.
JavaScript prototypes example
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 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