Created
February 21, 2015 14:23
-
-
Save eliza-abraham/09c2dc55115ca093c58b to your computer and use it in GitHub Desktop.
JavaScript Inheritance
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
// Constructors in prototypes | |
var Person = function(name) { | |
this.name = name; | |
} | |
var Student = function(name, university) { | |
// this.name = name // Wrong, no resusability; | |
// Person(name) // Wrong reference to this | |
Student.call(this, name); | |
this.university = university; | |
} | |
// Create a Student.prototype object that inherits from Person.prototype. | |
Student.prototype = Object.create(Person.prototype); | |
// Set the "constructor" property to refer to Student | |
Student.prototype.constructor = Student; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment