Advertisement
Guest User

Untitled

a guest
Apr 19th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class HandlerBase {
  2.     constructor(info) {
  3.         this.info = info;
  4.     }
  5.  
  6.     handler(action, value) {
  7.         switch (action) {
  8.             case 'changeName':
  9.                 this.info.name = value;
  10.                 break;
  11.             default:
  12.                 console.log('cannot match in default handler');
  13.         }
  14.     }
  15.  
  16.     a(test) {
  17.         console.log(test)
  18.     }
  19. }
  20.  
  21. class HobbiesHandler extends HandlerBase {
  22.     handler(action, value) {
  23.         // super(action, value);
  24.  
  25.         switch (action) {
  26.             case 'removeHobbies':
  27.                 this.info.hobbies = null;
  28.                 break
  29.             case 'addHobby':
  30.                 this.info.hobbies.push(value)
  31.                 break;
  32.             default:
  33.                 console.log('cannot match in any handlers');
  34.         }
  35.     }
  36. }
  37.  
  38. let info = {
  39.     name: 'Punn',
  40.     age: 22,
  41.     hobbies: [
  42.         'play games',
  43.         'read books'
  44.     ]
  45. };
  46.  
  47. let handlerMapping = {
  48.     'hobbies': HobbiesHandler
  49. }
  50.  
  51. function changeInfo(info, handlerName, action, value) {
  52.     let handlerClass = new (handlerMapping[handlerName] || HandlerBase)(info);
  53.    
  54.     handlerClass.handler(action, value);
  55. }
  56.  
  57. changeInfo(info, 'hobbies', 'addHobby', 'sing songs');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement