Created
January 11, 2017 19:16
-
-
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
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 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