There are moments where I miss multiple inheritance in languages such as javascript. Sometimes I wish I could access the functions from two different Scene-Classes from a derived class without the need to copy the entire code from one of them, cause everybody should know: "Duplicate code is bad". Then I remembered that I once saw a plugin creator just copying a function from one prototype to another like I did in the following code example with someFunction from Scene_B.prototype to Scene_C.prototype:
And via the call method I was also able to call a method from a prototype I even haven't derived from. I just wonder why and how is this possible and are there any drawbacks from doing that? Could someone please explain that to me!
JavaScript:
function Scene_A() { this.initialize.apply(this, arguments); }
Scene_A.prototype = Object.create(Scene_MenuBase.prototype);
Scene_A.prototype.constructor = Scene_A;
Scene_A.prototype.create = function() { Scene_MenuBase.prototype.create.call(this); this._testWindow = new Window_Base(0, 0, Graphics.boxWidth / 2, Graphics.boxHeight); this.addWindow(this._testWindow);}
function Scene_B() { this.initialize.apply(this, arguments); }
Scene_B.prototype = Object.create(Scene_MenuBase.prototype);
Scene_B.prototype.constructor = Scene_B;
Scene_B.prototype.create = function() { this._testWindow2 = new Window_Base(Graphics.boxWidth / 2, 0, Graphics.boxWidth / 2, Graphics.boxHeight); this.addWindow(this._testWindow2); }
Scene_B.prototype.someFunction = function() { alert("Hello World!"); }
function Scene_C() { this.initialize.apply(this, arguments);}
Scene_C.prototype = Object.create(Scene_A.prototype);
Scene_C.prototype.constructor = Scene_C;
Scene_C.prototype.someFunction = Scene_B.prototype.someFunction;
Scene_C.prototype.create = function() { Scene_A.prototype.create.call(this); Scene_B.prototype.create.call(this); this.someFunction(); }
SceneManager.push(Scene_C);
And via the call method I was also able to call a method from a prototype I even haven't derived from. I just wonder why and how is this possible and are there any drawbacks from doing that? Could someone please explain that to me!