Last active
October 17, 2016 18:58
-
-
Save halcarleton/0cb305ab804e8159b86ecf8b2448ad02 to your computer and use it in GitHub Desktop.
Super Constructor Pattern for future reference
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
// Base Class | |
function Base(p1, p2) { | |
this.x = this.someMethod(p1, p2); | |
this.y = { | |
a: p1, | |
b: p2 | |
}; | |
} | |
Base.prototype.someMethod = funciotn(a, b) { | |
return a + b; | |
}; | |
/*************/ | |
// SubClass that inherits from Base | |
function SubOne(p1) { | |
Base.call(this, 10, p1); | |
delete this.y.a; | |
}; | |
SubOne.prototype = Object.create(Base.prototype); | |
SubOne.prototype.constructor = SubOne; | |
/** | |
* new Base(2, 3) == { | |
* someMethod: Base.prototype.someMethod, | |
* x: 5, | |
* y: { | |
* a: 2, | |
* b: 3 | |
* } | |
* } | |
* | |
* new SubOne(8) == { | |
* someMethod: Base.prototype.someMethod, | |
* x: 80, | |
* y: { | |
* b: 8 | |
* } | |
* } | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment