Understanding the Module Pattern in JavaScript

Of all the design patterns you are likely to encounter in JavaScript, the module pattern is probably the most pervasive遍布的,充斥各处的. But it can also look a little strange to developers coming from other languages.

Let's walk through an example to see how it works. Suppose that we have a library of utility functions that looks something like this:

var batman = {
identity: "Bruce Wayne", fightCrime: function () {
console.log("Cleaning up Gotham.");
}, goCivilian: function () {
console.log("Attend social events as " + this.identity);
}
};

This version of batman is perfectly serviceable. It can fight crime when you call upon it. However, it has a serious weakness. This batman's identity property is publicly accessible.

Any code in your application could potentially overwrite it and cause batman to malfunction. For example:

// Some joker put this in your codebase
batman.identity = "a raving lunatic"; // Outputs: "Attend social events as a raving lunatic"
batman.goCivilian();

To avoid these sorts of situations we need a way to keep certain pieces of data private. Fortunately, JavaScript gives us just such a tool. We've even talked about it before: the immediately invoked function expression (IIFE).

A standard IIFE looks like this:

(function () {
// Code goes here
})();

The advantage of the IIFE is that any vars declared inside it are inaccessible to the outside world. So how does that help us? The key is that an IIFE can have a return value just like any other function.

var batman = (function () {
var identity = "Bruce Wayne"; return {
fightCrime: function () {
console.log("Cleaning up Gotham.");
}, goCivilian: function () {
console.log("Attend social events as " + identity);
}
};
})();
// Outputs: undefined
console.log(batman.identity); // Outputs: "Attend social events as Bruce Wayne"
batman.goCivilian();

As you can see, we were able to use the IFFE's return value to make batman's utility functions publicly accessible. And at the same time we were able to ensure that batman's identityremains a secret from any clowns who want to mess with it.

You might be wondering when using the module pattern is a good idea. The answer is that it works well for situations like the one illustrated here. If you need to both enforce privacy for some of your data and provide a public interface, then the module pattern is probably a good fit.

It is worth considering, though whether you really need to enforce data privacy, or whether using a naming convention to indicate private data is a better approach. The answer to that question will vary on a case by case basis. But now you're equipped to enforce data privacy if necessary.

Thanks for reading!

Josh Clanton

扩展阅读

Javascript中的Module(模块)模式

最新文章

  1. Nginx+PHP On windows
  2. Xcode8更新CocoaPods报错解决办法
  3. gulp配置文件备份
  4. Android中Adapter之BaseAdapter使用
  5. eclipse中的Console控制台视图脱离主窗口解决办法
  6. .net平台下深拷贝和浅拷贝
  7. IO/NIO
  8. 添加删除ASM磁盘
  9. 前端学习——JQuery Ajax使用经验
  10. Memcached基本架构和思想
  11. JSP文件上传--FileUpload组件
  12. Linux中断子系统:级联中断控制器驱动
  13. 201621123002《Java程序设计》第十一周学习总结
  14. 升讯威微信营销系统开发实践:订阅号和服务号深入分析( 完整开源于 Github)
  15. Hello Flask
  16. 一个简单的特效引发的大战之移动开发中我为什么放弃jquery mobile
  17. FFmpeg数据结构AVBuffer
  18. [pat]A1072 Gas Station
  19. Android提示版本更新的实现
  20. hdu - 6277,2018CCPC湖南全国邀请赛B题,找规律,贪心找最优.

热门文章

  1. Pycharm2019.1.3破解
  2. [LeetCode] 103. 二叉树的锯齿形层次遍历
  3. seajs与requirejs
  4. 解决Ubuntu环境下在pycharm中导入tensorflow报错问题
  5. Apache 用户认证
  6. Linux服务器安装系统之1-LSI阵列卡Raid10配置方法
  7. 012-linux系统管理——进程管理与工作管理
  8. 05java基础
  9. 【串线篇】spring boot启动配置原理
  10. Windows Server2008R2蓝屏,分析dmp文件