Last active
April 16, 2018 10:33
-
-
Save EngPeterShaker/bbde5ab7da3b6629b9903470181d7921 to your computer and use it in GitHub Desktop.
a gist describing function inheritance using three kinds [new , .prototype , util.inherits(constructor , super constructor) ] .. and extending two child objects with [.call() and .apply()]
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 util = require('util'); | |
function Person(firstname, lastname) { | |
this.firstname = firstname; | |
this.lastname = lastname; | |
} | |
Person.prototype.greet = function() { | |
console.log('Hello ' + this.firstname + ' ' + this.lastname); | |
console.log('badge number:'+this.badgenumber); | |
}; | |
function Policeman() { | |
Person.call(this, 'peter' , 'policeman'); | |
this.badgenumber = '1234'; | |
} | |
function colonel() { | |
Person.apply(this, ['peter', 'colonel']); | |
this.badgenumber = '9999'; | |
} | |
util.inherits(Policeman, Person); | |
util.inherits(colonel, Person); | |
var officer = new Policeman(); | |
var officer2 = new colonel(); | |
officer.greet(); | |
officer2.greet(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
officer._proto_._proto_ = greet()
// a prototype of a prototype