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 :: javascript canvas load image 
Javascript :: javascript fadeout without jquery 
Javascript :: data table buttons 
Javascript :: javascripts events 
Javascript :: add 7 days in date using jquery 
Javascript :: audio get current time 
Javascript :: javascript save multiple images to server 
Javascript :: how to write unit test cases in react js 
Javascript :: loop for of 
Javascript :: react-native-geolocation-service 
Javascript :: how to get length in js without using .length method 
Javascript :: discord.js give role command 
Javascript :: javascript casting objects 
Javascript :: javascript get last element in array 
Javascript :: js date toisostring with timezone 
Javascript :: npm passport-instagram 
Javascript :: filter 2d array javascript 
Javascript :: javascript javascript javascript javascript javascript 
Javascript :: got back to start of for loop js 
Javascript :: javascript detect time on page 
Javascript :: how to add multiple videos in html5 with javascript 
Javascript :: delete JSON properties in place with jq 
Javascript :: (function (g, d, a) {})(window, document, jQuery); 
Javascript :: timeout for javascript 
Python :: matplotlib change thickness of line 
Python :: python iterate through date range 
Python :: vowel and consonant list python 
Python :: string to datetime convert 
Python :: pytube.exceptions.RegexMatchError: get_throttling_function_name: could not find match for multiple 
Python :: resize imshow opencv python 
ADD CONTENT
Topic
Content
Source link
Name
1+6 =