Class Augmenter
Demo
Open your browser's console and reload the page to see the code in action.
Register the Class
<script src="AcidJs.Augmenter.js"></script>
Usage and Code Insight
(function() { window.augmenter = new window.AcidJs.Augmenter(); function Superclass(config) { config = config || {}; this.config = config; this.prop1 = "property 1"; this.prop2 = "property 2"; this.prop3 = "property 3"; } Superclass.prototype = { m1: function() { console.log("m1"); }, m2: function() { console.log("m2"); }, m3: function() { console.log("m3"); } }; window.Superclass = Superclass; function Class1() { } Class1.prototype = { init: function() { console.log("Class1 init()"); this.m1(); } }; window.Class1 = Class1; function Class2() { } Class2.prototype = { init: function() { console.log("Class2 init()"); this.m1(); } }; window.Class2 = Class2; window.sc = new window.Superclass({ name: "martin", secondName: "ivanov" }); window.augmenter.methods(Superclass, Class1, ["m1", "m2"]); window.augmenter.methods(window.Superclass, window.Class2, ["m1", "m2"]); window.augmenter.properties(Superclass, Class1, ["prop1", "prop2"]); window.augmenter.properties(window.Superclass, window.Class2); window.cl1 = new Class1(); window.cl2 = new window.Class2(); window.augmenter.properties(window.sc, window.cl1, ["prop2", "prop3"]); window.augmenter.properties(window.sc, window.cl2); window.cl1.init(); window.cl2.init(); })();
(function() { "use strict"; /* * @namespace AcidJs **/ if(undefined === window.AcidJs) { window.AcidJs = {}; } /* * @class Augmenter * @constructor **/ function Augmenter() {} Augmenter.prototype = { /* * @method methods Inherit methods from one class to another * @pulic * @param superclass {Class} * @param subclass {Class} * @param members {Array} [optional] Array of methods that should be inherited. If not set, all superclass methods will be copied to the subclass **/ methods: function(superclass, subclass, members) { var member; if(members) { for(var i = 0; i < members.length; i ++) { member = members[i]; if(superclass.prototype.hasOwnProperty(member)) { subclass.prototype[member] = superclass.prototype[member]; } } } else { for(member in superclass.prototype) { if(superclass.prototype.hasOwnProperty(member)) { subclass.prototype[member] = superclass.prototype[member]; } } } }, /* * @properties properties Inherit properties from one class to another * @pulic * @param superclass {Class} * @param subclass {Class} * @param properties {Array} [optional] Array of properties that should be inherited. If not set all superclass properties will be copied to the subclass **/ properties: function(superclass, subclass, properties) { if(properties) { for(var i = 0; i < properties.length; i ++) { if(superclass.hasOwnProperty(properties[i])) { subclass[properties[i]] = superclass[properties[i]]; } } } else { for(var property in superclass) { if(superclass.hasOwnProperty(property)) { subclass[property] = superclass[property]; } } } } }; window.AcidJs.Augmenter = Augmenter; })();