Created
January 7, 2016 18:52
-
-
Save sax1johno/ee17a86cd2ff8232488c to your computer and use it in GitHub Desktop.
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
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