Last active
September 13, 2018 20:53
-
-
Save pixelsnob/bf5a739218e4df3ecb32530e60cf73db to your computer and use it in GitHub Desktop.
Simulated "classical inheritance" in JS
This file contains hidden or 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
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