Created
November 6, 2012 09:23
-
-
Save ricca509/4023657 to your computer and use it in GitHub Desktop.
Prototypal inheritance
This file contains hidden or 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
// Define a new Constructor function | |
var Coffee = function(type) { | |
this.type = type; | |
} | |
// Add a property to our object | |
Coffee.prototype.taste = 'sweet'; | |
// Create an instance of Coffee | |
// This instance inherits all the properties of Coffee | |
var espresso = new Coffee('espresso'); | |
console.log(espresso.taste); | |
// output : "sweet" | |
// Change the property with a new value | |
espresso.taste = 'creamy'; | |
console.log(espresso.taste); | |
// output : "creamy" | |
// Now, remove the 'taste' property from our instance | |
delete espresso.taste; | |
// JavaScript attempts to retrieve the property value | |
// from the prototype object (Coffee) | |
console.log(espresso.taste); | |
// output : "sweet" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment