Skip to content

Instantly share code, notes, and snippets.

@eliza-abraham
Created February 21, 2015 14:23
Show Gist options
  • Save eliza-abraham/09c2dc55115ca093c58b to your computer and use it in GitHub Desktop.
Save eliza-abraham/09c2dc55115ca093c58b to your computer and use it in GitHub Desktop.
JavaScript Inheritance
// 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