kstoyanov

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

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