Created
October 12, 2016 23:11
-
-
Save alinarezrangel/9c8e959dbbdeca5fae2a93b77eb71d9e to your computer and use it in GitHub Desktop.
Revisions
-
alinarezrangel created this gist
Oct 12, 2016 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,65 @@ function Extends(base, child) { 'use strict'; if((typeof base !== 'object') || (typeof child !== 'object')) { throw new TypeError('Error at Extend(base, child): the base or the child arent objects'); } child._super = {}; for(let i in base) { if(base.hasOwnProperty(i) && (typeof child[i] !== 'undefined')) { if(typeof base[i] === 'object') { child._super[i] = Extends(base[i], {}); } else if(typeof base[i] == 'function') { child._super[i] = base[i].bind(child); } else { child._super[i] = base[i]; } continue; } if(base.hasOwnProperty(i) && (typeof child[i] === 'undefined')) { if(typeof base[i] === 'object') { child[i] = Extends(base[i], {}); } else if(typeof base[i] == 'function') { child[i] = base[i].bind(child); } else { child[i] = base[i]; } } } return child; } function Class(template) { 'use strict'; template._super = template._super || {}; template._constructor = template._constructor || function() {}; return template; } function New(cls) { 'use strict'; var args = []; for(let i = 1; i < arguments.length; i++) { args.push(arguments[i]); } var obj = Object.create(cls); obj._constructor.apply(obj, args); return obj; } 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,42 @@ var Widget = Class({ // Public and shared attributes here className: 'Widget', htmlTag: 'div', _constructor: function(attrs) { // "Private" and custom attributes here this.attributes = attrs; }, toString: function() { var st = ''; var i; for(i in this.attributes) { if(this.attributes.hasOwnProperty(i)) { st += i + '=' + this.attributes[i] + '\n'; } } return st; } }); var Button = Class(Extends(Widget, { className: 'Button', htmlTag: 'button', _constructor: function(attrs) { this._super._constructor(attrs); }, toString: function() { return 'Button: ' + this._super.toString(); } })); var widget1 = New(Widget, {hello: 'world'}); var button1 = New(Button, {hello: 'world'}); console.log(widget1.toString()); console.log(button1.toString());