DEV Community

Discussion on: Revealing Module Pattern in Javascript

Collapse
pat_the99 profile image
Patricia Nicole Author

Wow thank you for this peerreynders!!

Collapse
peerreynders profile image
peerreynders

... come to think of it, as there is no reliance on this, the return value doesn't have to be an object anymore.

const initialValue = 0;
const valueTuple = (function (init) {
  let val = init;

  const getValue = () => val;
  const setValue = (newVal) => {
    val = newVal;
  };

  return [getValue, setValue];
})(initialValue);

const [current, set] = valueTuple;
console.assert(current() === initialValue, 'initialization failed');
set(1);
console.assert(current() === 1, 'set to 1 failed');
set(3);
console.assert(current() === 3, 'set to 3 failed');
Enter fullscreen mode Exit fullscreen mode

Though perhaps now we aren't revealing a "module" anymore.