Created
October 12, 2016 23:11
-
-
Save alinarezrangel/9c8e959dbbdeca5fae2a93b77eb71d9e to your computer and use it in GitHub Desktop.
Fake JavaScript classes (ES5 or ES6)
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
| 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 characters
| 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()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment