Last active
August 29, 2015 14:11
-
-
Save kmaida/69421b72678a9bf35a2a to your computer and use it in GitHub Desktop.
JavaScript prototypal inheritance example - http://stackoverflow.com/questions/387707/whats-the-best-way-to-define-a-class-in-javascript
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 a class like this | |
function Person(name, gender) { | |
// Add object properties like this | |
this.name = name; | |
this.gender = gender; | |
} | |
// Add methods like this. All Person objects will be able to invoke this | |
Person.prototype.speak = function() { | |
alert('Howdy, my name is ' + this.name); | |
}; | |
// Instantiate new objects with 'new' | |
var Bob = new Person('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