Advertisement
kstoyanov

05. Sum - using the "Module" pattern

Oct 12th, 2020
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  function getModel() {
  2.     const model = (() => ({
  3.       init(selector1, selector2, resultSelector) {
  4.         model.num1 = document.querySelector(`${selector1}`);
  5.         model.num2 = document.querySelector(`${selector2}`);
  6.         model.result = document.querySelector(`${resultSelector}`);
  7.       },
  8.       add: () => model.action((a, b) => a + b),
  9.       subtract: () => model.action((a, b) => a - b),
  10.       action(operation) {
  11.         const val1 = Number(model.num1.value);
  12.         const val2 = Number(model.num2.value);
  13.         model.result.value = operation(val1, val2);
  14.       },
  15.     }))();
  16.  
  17.     return model;
  18.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement