Skip to content

Instantly share code, notes, and snippets.

@kmaida
Last active August 29, 2015 14:11
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 kmaida/e6d0208e00a7da6e8073 to your computer and use it in GitHub Desktop.
Save kmaida/e6d0208e00a7da6e8073 to your computer and use it in GitHub Desktop.
JavaScript inheritance with objects - http://davidwalsh.name/javascript-objects-deconstruction
// Define an object with init function and add methods
var Person = {
init: function(name, gender) {
this.name = name;
this.gender = gender;
},
speak: function() {
alert('Howdy, my name is ' + this.name);
}
};
// Instantiate new objects with Object.create
var Bob = Object.create(Person);
Bob.init('Bob', 'M');
// Invoke methods like this
Bob.speak(); // alerts 'Howdy, my name is Bob'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment