Skip to content

Latest commit

 

History

History
51 lines (47 loc) · 1.18 KB

extract-module.md

File metadata and controls

51 lines (47 loc) · 1.18 KB

Extract module

You have a collection of related functions that can be utilised through a simple interface.

var getPostsFromDataSource = function() {
    // Fetch posts from data source
};
var logPosts = function(posts) {
    // Log each post to console
};
var mapPosts = function(posts) {
    // Map each post to a model
};
var getPosts = function() {
    var posts = getPostsFromDataSource();
    posts = mapPosts(posts);
    logPosts(posts);
    return posts;
};
var posts = getPosts();

Encapsulate the functions in a new function and return a fine-grained interface for clients.

var postsModule = function() {
  var getPostsFromDataSource = function() {
      // Fetch posts from data source
  };
  var logPosts = function(posts) {
      // Log each post to console
  };
  var mapPosts = function(posts) {
      // Map each post to a model
  };
  var getPosts = function() {
      var posts = getPostsFromDataSource();
      posts = mapPosts(posts);
      logPosts(posts);
      return posts;
  };
  return {
  	getPosts: getPosts
  };
}();
var posts = postsModule.getPosts();

More Information