less than 1 minute read

ES5 Basic Module Pattern.

Place public functions in the return statement.

const moduleName = (function () {
  const _private = "something";
  const _privateFunction = function () {
    return;
  };
  const publicFunction = function () {
    return;
  };
  return {
    publicFunction: publicFunction, // This function will expose
  };
})();

// Example
moduleName._privateFunction(); // Invalid
moduleName.publicFunction(); // Valid

Categories:

Updated:

Comments