Skip to content

Instantly share code, notes, and snippets.

@vadym1930
Created January 11, 2017 19:16
Show Gist options
  • Save vadym1930/7730e2631c7697b766372935ea303cb4 to your computer and use it in GitHub Desktop.
Save vadym1930/7730e2631c7697b766372935ea303cb4 to your computer and use it in GitHub Desktop.
inherits function ( from https://github.com/nodejs/node-v0.x-archive/blob/master/lib/util.js#L634-L644), OOP classical pattern example
function inherits(ctor, superCtor) {
ctor.super_ = superCtor;
ctor.prototype = Object.create(superCtor.prototype, {
constructor: {
value: ctor,
enumerable: false,
writable: true,
configurable: true
}
});
};
var Person = function(name) {
this.name = name;
}
Person.prototype.logName = function() {
console.log("hi, I'm " + this.name);
}
var bob = new Person("bobby");
var john = new Person("jonny");
var Friend = function(name, hobby) {
Friend.super_.call(this, name);
this.hobby = hobby;
}
inherits(Friend, Person);
Friend.prototype.getHobby = function() {
console.log(this.hobby);
}
Friend.prototype.logName = function() {
console.log("Hello, I'm " + this.name);
}
var veronica = new Friend("veronica", "dancing");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment