Skip to content

Instantly share code, notes, and snippets.

@sax1johno
Created January 7, 2016 18:52
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 sax1johno/ee17a86cd2ff8232488c to your computer and use it in GitHub Desktop.
Save sax1johno/ee17a86cd2ff8232488c to your computer and use it in GitHub Desktop.
var Student = function(name, course, pace) {
this.name = name;
this.course = course;
this.pace = pace;
this.printStudent = function() {
console.log(this.name, this.course, this.pace);
}
}
Student.prototype.printStudent = function() {
console.log(this.name, this.course, this.pace);
}
// Student.printStudent(); // BAD, don't do this.
var me = new Student("Dean", "frontend", "18-week");
me.printStudent();
// This is a class method rather than an instance method
Student.printClass = function() {
return "Student";
}
// me.printClass() // bad
Student.printClass() // Correct.
// How we get standard inheritance
// This creates a GradStudent that has the same properties as a Student.
var GradStudent = function() {
// Same as calling super();
// Calls the constructor of my parent class.
Student.call(this);
}
// Setting the parent of gradstudent to student.
// Do that by setting the prototypes equal to each other.
GradStudent.prototype = Object.create(Student.prototype);
var john = new GradStudent("John", "test", "24-week");
// Even though "name" is defined in student, grad student now has a "name"
// property because of the inheritance.
if (GradStudent.hasOwnProperty("printStudent")) {
// console.log("Grad student had property we were looking for");
john.printStudent();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment