- JavaScript:Moving to ES2015
- Ved Antani Simon Timms Narayan Prusty
- 417字
- 2021-07-09 19:07:33
Modules
Modules are used to mimic classes and focus on public and private access to variables and functions. Modules help in reducing the global scope pollution. Effective use of modules can reduce name collisions across a large code base. A typical format that this pattern takes is as follows:
Var moduleName=function() { //private state //private functions return { //public state //public variables } }
There are two requirements to implement this pattern in the preceding format:
- There must be an outer enclosing function that needs to be executed at least once.
- This enclosing function must return at least one inner function. This is necessary to create a closure over the private state—without this, you can't access the private state at all.
Check the following example of a module:
var superModule = (function (){ var secret = 'supersecretkey'; var passcode = 'nuke'; function getSecret() { console.log( secret ); } function getPassCode() { console.log( passcode ); } return { getSecret: getSecret, getPassCode: getPassCode }; })(); superModule.getSecret(); superModule.getPassCode();
This example satisfies both the conditions. Firstly, we create an IIFE or a named function to act as an outer enclosure. The variables defined will remain private because they are scoped in the function. We return the public functions to make sure that we have a closure over the private scope. Using IIFE in the module pattern will actually result in a singleton instance of this function. If you want to create multiple instances, you can create named function expressions as part of the module as well.
We will keep exploring various facets of functional aspects of JavaScript and closures in particular. There can be a lot of imaginative uses of such elegant constructs. An effective way to understand various patterns is to study the code of popular libraries and practice writing these patterns in your code.
Stylistic considerations
As in the previous chapter, we will conclude this discussion with certain stylistic considerations. Again, these are generally accepted guidelines and not rules—feel free to deviate from them if you have reason to believe otherwise:
- Use function declarations instead of function expressions:
// bad const foo = function () { }; // good function foo() { }
- Never declare a function in a non-function block (if, while, and so on). Assign the function to a variable instead. Browsers allow you to do it, but they all interpret it differently.
- Never name a parameter
arguments
. This will take precedence over thearguments
object that is given to every function scope.
- Spring Boot開發與測試實戰
- Mastering ServiceStack
- Visual Basic程序設計教程
- Building RESTful Python Web Services
- Webpack實戰:入門、進階與調優
- Mastering Python Design Patterns
- 編寫高質量代碼:改善Objective-C程序的61個建議
- R Data Science Essentials
- Programming Microsoft Dynamics? NAV 2015
- 區塊鏈架構之美:從比特幣、以太坊、超級賬本看區塊鏈架構設計
- Unity 2017 Game AI Programming(Third Edition)
- PhoneGap 4 Mobile Application Development Cookbook
- C# 7.0本質論
- The Ruby Workshop
- Visual FoxPro程序設計教程