Smipple is shutting down Nov 30, 2017. Some features are currently disabled.
Profile image for Klaus Kazlauskas klausmachado
Extend classes
Language
JavaScript
Tags
class extend javascript

Extending classes without using prototype

1 function valueType(value) 2 { 3 switch(typeof value) { 4 case 'string': 5 return eval('\'value\''); 6 break; 7 8 case default: 9 return value; 10 break; 11 } 12 } 13 14 function extend(originalClass, targetClass, parameters) 15 { 16 var extendedClass = '', 17 parametersValues = ''; 18 19 if(!!parameters) { 20 switch(typeof parameters) { 21 case 'object': 22 for(var index in parameters) { 23 if(!!parametersValues) { 24 parametersValues += ', '; 25 } 26 parametersValues += valueType(eval('parameters.' + index)); 27 } 28 break; 29 30 case 'string': 31 parametersValues = parameters; 32 break; 33 34 case 'array': 35 for(var index in parameters) { 36 if(!!parametersValues) { 37 parametersValues += ', '; 38 } 39 parametersValues += valueType(parameters[index]); 40 } 41 break; 42 } 43 } 44 45 eval('extendedClass = new ' + targetClass + '(' + parametersValues + ');'); 46 for(var object in extendedClass) { 47 originalClass.prototype.{object} = function(){ }; 48 eval('originalClass.' + object + ' = extendedClass.' + object + ';'); 49 } 50 } 51 52 function Opa() 53 { 54 this.haha = 'sim'; 55 this.eumesmo = function() { return true; }; 56 } 57 58 function Teste() 59 { 60 extend(this, 'Opa'); 61 this.haha = 'nao'; 62 } 63 64 var apenas = new Teste(); 65 66 console.log(apenas);

Comments