Skip to content

Instantly share code, notes, and snippets.

@ricca509
Created November 6, 2012 09:23
Show Gist options
  • Save ricca509/4023657 to your computer and use it in GitHub Desktop.
Save ricca509/4023657 to your computer and use it in GitHub Desktop.
Prototypal inheritance
// 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