rogerxu / rogerxu.github.io

Roger Xu's Blog
3 stars 2 forks source link

Revealing Module Pattern #84

Open rogerxu opened 7 years ago

rogerxu commented 7 years ago

Revealing Module - Learning JavaScript Design Patterns

rogerxu commented 7 years ago
(function() {
  let privateVar = 'secret';

  function privateFunction() {
    console.log(`Name: ${privateVar}`);
  }

  function publicSetName(name) {
    privateVar = name;
  }

  function publicGetName() {
    return privateFunction();
  }

  return {
    setName: publicSetName,
    getName: publicGetName,
  };
}());