Skip to content

Instantly share code, notes, and snippets.

@pixelsnob
Last active September 13, 2018 20:53
Show Gist options
  • Save pixelsnob/bf5a739218e4df3ecb32530e60cf73db to your computer and use it in GitHub Desktop.
Save pixelsnob/bf5a739218e4df3ecb32530e60cf73db to your computer and use it in GitHub Desktop.
Simulated "classical inheritance" in JS
const Parent = function(x) {
this.x = x;
console.log('Parent called');
};
const Child = function() {
Parent.call(this, 'xxxxx');
console.log('Child called');
};
Child.prototype.setX = function(x) {
this.x = x;
};
Child.prototype = Object.create(Parent.prototype);
Child.prototype.constructor = Child;
const child = new Child;
child.x = '5dsfdadss';
console.log(child);
const SomeMixin = function() {
console.log('Mixin called');
this.mixin_val = 't';
};
SomeMixin.prototype.test = function() {
this.mixin_val = 'test';
};
const Child2 = function() {
this.child2_val = 'xxxxxxxxxxxx';
SomeMixin.call(this);
};
Child2.prototype = Object.create(Child.prototype);
Child2.prototype.constructor = Child2;
Child2.prototype = Object.assign(Child2.prototype, SomeMixin.prototype);
const child2 = new Child2;
child2.test();
console.log(child2);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment