Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

javascript module pattern

var counter = (function() {
  var privateCounter = 0;
  function changeBy(val) {
    privateCounter += val;
  }

  return {
    increment: function() {
      changeBy(1);
    },

    decrement: function() {
      changeBy(-1);
    },

    value: function() {
      return privateCounter;
    }
  };
})();

console.log(counter.value());  // 0.

counter.increment();
counter.increment();
console.log(counter.value());  // 2.

counter.decrement();
console.log(counter.value());  // 1.
Comment

js module pattern

const Formatter = (function() {
  const log = (message) => console.log(`[${Date.now()}] Logger: ${message}`);

  const makeUppercase = (text) => {
    log("Making uppercase");
    return text.toUpperCase();
  };  

  return {
    makeUppercase,
  }
})();
Comment

The Module design pattern in javascript

var testModule = (function() {
  var counter = 0;
  return {
    incrementCounter: function() {
      return ++counter;
    },
    resetCounter: function() {
      counter = 0;
    }
  };
})();

// Usage:
testModule.incrementCounter();
testModule.resetCounter();
Comment

PREVIOUS NEXT
Code Example
Javascript :: post request javascript 
Javascript :: post request with authorization 
Javascript :: upi id regex 
Javascript :: angular convert map values to array 
Javascript :: sliding window algorithm javascript 
Javascript :: js random unique id 
Javascript :: read json file into array javascript 
Javascript :: remove eslint check react native 
Javascript :: regx to accept name 
Javascript :: invoking jquery validator 
Javascript :: how to set default value in input field in angularjs 
Javascript :: react before css 
Javascript :: hti laravel route from javascript file 
Javascript :: how to clear input value in antdesign form on submit 
Javascript :: Nestjs download 
Javascript :: react cdn link 
Javascript :: javascript access ajax response headers 
Javascript :: jspdf 
Javascript :: get index vanilla js 
Javascript :: lodash find duplicate element index 
Javascript :: react-bootstrap problem-install new version 
Javascript :: divider in react native 
Javascript :: node.js check if a remote URL exists 
Javascript :: ant design charts 
Javascript :: how to add all values of array together js 
Javascript :: image onclick react 
Javascript :: buffer to image nodejs 
Javascript :: circular queue implementation using js 
Javascript :: canvas set image height 
Javascript :: javascript integer 
ADD CONTENT
Topic
Content
Source link
Name
6+9 =