Last active
August 29, 2015 14:11
-
-
Save kmaida/e6d0208e00a7da6e8073 to your computer and use it in GitHub Desktop.
JavaScript inheritance with objects - http://davidwalsh.name/javascript-objects-deconstruction
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
// 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