Skip to content

Instantly share code, notes, and snippets.

@loburets
Last active October 24, 2018 15:42
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 loburets/e6b2194a437813a5ad73832a74210021 to your computer and use it in GitHub Desktop.
Save loburets/e6b2194a437813a5ad73832a74210021 to your computer and use it in GitHub Desktop.
ES5 js multiple inheritance example
function Machine() {
this._enabled = false;
this.enable = function() {
this._enabled = true;
};
this.disable = function() {
this._enabled = false;
};
}
function Shape() {
var type = '3d';
this.getType = function() {
return type;
};
}
function CoffeeMachine(power) {
// call the constructor functions with current context
// all actions of the constructors will be applied for CoffeeMachine this
Machine.call(this);
Shape.call(this);
this.name = "Brown";
}
cf = new CoffeeMachine(1);
console.log(cf.type); // undefined
console.log(cf.getType()); // 3d
cf.enable();
console.log(cf._enabled); // true
console.log(cf.name); // brown
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment