Skip to content

Instantly share code, notes, and snippets.

@EngPeterShaker
Last active April 16, 2018 10:33
Show Gist options
  • Save EngPeterShaker/bbde5ab7da3b6629b9903470181d7921 to your computer and use it in GitHub Desktop.
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()]
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();
@EngPeterShaker
Copy link
Author

EngPeterShaker commented Apr 16, 2018

officer._proto_._proto_ = greet() // a prototype of a prototype

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment