Skip to content

Instantly share code, notes, and snippets.

@ScottAwesome
Created June 11, 2020 22:08
Show Gist options
  • Save ScottAwesome/67d2d10761bd4993eec9d9d63b0f5b9e to your computer and use it in GitHub Desktop.
Save ScottAwesome/67d2d10761bd4993eec9d9d63b0f5b9e to your computer and use it in GitHub Desktop.
Demonstrating Old Style JS Inheritance Non Obviousness
// I know its contrived, but you get the gist :)
'use strict';
function BasePrototype() {
this.mary = 'Mary';
this.lamb = true;
this.little = false;
}
BasePrototype.prototype.had = function() {
return true;
};
function Little() {
// Remember this technique?
if (!(this instanceof Little)) {
return new Little();
}
// Now we have inheritance: not super obvious, per se, especially if this was a larger file, or there was
// a lot more going on inside of the 'constructor'. There's no normal happy path to do this.
// Also, if you add this *before* the above checking instance, it will fail. Its very fragile
// It was also (and still is) super common.
BasePrototype.call(this);
this.fleece = 'white';
this.as = 'snow';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment