Last active
December 25, 2015 03:59
-
-
Save Gwash3189/6914454 to your computer and use it in GitHub Desktop.
Prototypical inheritance in NodeJs
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
function Person(name){ | |
this.name = name; | |
} | |
module.exports = Person; |
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 Person = require('./Person.js') | |
function Student(name, studentNumber){ | |
Person.call(this, name); | |
this.studentNumber = studentNumber; | |
} | |
Student.prototype = Object.create(Person.prototype); | |
Student.prototype.constructor = Student; | |
module.exports = Student; |
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 Person = require('./Person.js'); | |
var Student = require('./Student.js'); | |
var joe = new Person('Joe'); | |
var smith = new Student('Smith', 1234); | |
if(smith instanceof Student){ | |
console.log('instance of student'); | |
} | |
if(smith instanceof Person){ | |
console.log('instance of person'); | |
} | |
if(joe instanceof Student === false){ | |
console.log('not an instance of student'); | |
} | |
if(joe instanceof Person){ | |
console.log('but is an instance of a person'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment