kstoyanov

05. Sum - v2 - using the "Revealing Module" pattern

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