Skip to content

Instantly share code, notes, and snippets.

@Gwash3189
Last active December 25, 2015 03:59
Show Gist options
  • Save Gwash3189/6914454 to your computer and use it in GitHub Desktop.
Save Gwash3189/6914454 to your computer and use it in GitHub Desktop.
Prototypical inheritance in NodeJs
function Person(name){
this.name = name;
}
module.exports = Person;
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;
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