Skip to content

Instantly share code, notes, and snippets.

@halcarleton
Last active October 17, 2016 18:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save halcarleton/0cb305ab804e8159b86ecf8b2448ad02 to your computer and use it in GitHub Desktop.
Save halcarleton/0cb305ab804e8159b86ecf8b2448ad02 to your computer and use it in GitHub Desktop.
Super Constructor Pattern for future reference
// 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