Guest User

Untitled

a guest
Jun 25th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.01 KB | None | 0 0
  1. Object.prototype.attachClass = function(name, o){
  2.  
  3. this[name] = function(){
  4. if(typeof(this.__init) == 'function'){
  5. this.__init.apply(this, arguments);
  6. }
  7. };
  8.  
  9. var new_methods = {};
  10. for(var m in o){
  11. if(typeof(o[m]) == 'function'){
  12. eval('new_methods[m] = ' + o[m].toString().replace(') {', '){ var self = this;'));
  13. }
  14. else {
  15. new_methods[m] = o[m];
  16. }
  17. }
  18.  
  19. this[name].prototype = new_methods;
  20.  
  21. }
  22.  
  23. var Global = {};
  24. Global.attachClass('Person', {
  25.  
  26. bodyType: 'alien',
  27.  
  28. __init: function(opts){
  29. self.age = opts.age;
  30. },
  31.  
  32. getAge: function(){
  33.  
  34. // OMG, ALIENS ARE 10 TIMES OLDER THAN US!
  35. function verifyAge(age){
  36. if(self.bodyType == 'alien'){
  37. return Number(age) * 10;
  38. }
  39. return age;
  40. }
  41.  
  42. return verifyAge(self.age);
  43. }
  44.  
  45. });
  46.  
  47. var p = new Global.Person({ age: 34 });
  48. console.log(p.getAge());
Add Comment
Please, Sign In to add comment