JavaScript
Module Pattern
🚀🔥🚀
Â
JavaScript Module Pattern
Â
- Scoping in JavaScript
- What is the Module Pattern?
- Declare a Module in 2 ways
- How to use a Module in another Module?
- Why use the Module Pattern?
- Sources!! 💻
Â
Scoping in JavaScript
Â
What is a scope?
Â
(function() {
// this is a scope
})();
function util() {
// this is a scope
}
var util = function() {
// this is a scope
}
A scope in JavaScript is created by a Function
Â
Global scope
Â
var global = 'Global scope';
// or
window.global = 'Global scope';
Local scope
Â
(function() {
var local = 'Local scope';
})();
function simpleFunction() {
var local = 'Local scope';
}
Public & private
Â
var globalVar = 'Global Variable';
(function() {
console.log('globalVar: ', globalVar);
var localVar = 'Local scope';
})();
console.log('localVar: ', localVar);
console.log('globalVar: ', globalVar);
What wil be the error?
Â
Public & private
Â
var globalVar = 'Global Variable';
(function() {
console.log('globalVar: ', globalVar);
var localVar = 'Local scope';
})();
console.log('localVar: ', localVar); // undefined
console.log('globalVar: ', globalVar); // 'Global Variable'
// Uncaught ReferenceError: localVar is not defined
The answer is:
Â
Public & private (advanced)
Â
var globalVar = 'Global Variable';
(function() {
var localVar = 'Local scope';
(function() {
var childVar = 'Child scope';
console.log('childVar: ', childVar); // 'Child scope'
console.log('localVar: ', localVar); // 'Local scope'
console.log('globalVar: ', globalVar); // 'Global Variable'
})();
console.log('childVar: ', childVar); // undefined
console.log('localVar: ', localVar); // 'Local scope'
console.log('globalVar: ', globalVar); // 'Global Variable'
})();
console.log('childVar: ', childVar); // undefined
console.log('localVar: ', localVar); // undefined
console.log('globalVar: ', globalVar); // 'Global Variable'
What is the Module Pattern?
Â
What is the Module Pattern?
Â
It is a JavaScript Design pattern that gives more structure (with private and public methods) to a JavaScript application.
Â
JavaScript Module
Â
var Module = (function () {
// code
})();
Private & Public Methods
Â
var Module = (function () {
var _privateMethod = function() {
// Private method
}
return {
publicMethod: function() {
// Public method
}
}
})();
Call private methods
Â
var Module = (function () {
var _privateMethod = function() {
// Private method
}
})();
Module._privateMethod(); // undefined
Call public methods
Â
var Module = (function () {
// ... methods
return {
publicMethod: function() {
// Public method
return 'JavaScript Module Pattern';
}
}
})();
Module.publicMethod(); // 'JavaScript Module Pattern'
Use private methods
Â
var Module = (function () {
var _privateMethod = function() {
// Private method
return 'JavaScript Module Pattern';
}
return {
publicMethod: function() {
// Public method
return _privateMethod();
}
}
})();
Module.publicMethod(); // 'JavaScript Module Pattern'
Declare a module in 2 ways
Â
Locally scoped Object Literal
Â
var Module = (function () {
var localObject = {};
var _privateMethod = function() {
// Private method
}
localObject.publicMethod = function() {
// Public method
}
return localObject;
})();
Revealing Module Pattern
Â
var Module = (function () {
var methodOne = function() {
}
var methodTwo = function() {
}
return {
publicMethodOne: methodOne,
publicMethodTwo: methodTwo
}
})();
How to use a Module in another Module?
Â
var Module = (function () {
var localObject = {};
var _privateMethod = function() {
// Private method
}
localObject.publicMethod = function() {
// Public method
}
return localObject;
})();
var Module2 = (function (Module) {
var localObject = {};
var _privateMethod = function() {
// Private method
Module._privateMethod();
}
localObject.publicMethod = function() {
// Public method
}
return localObject;
})(Module);
Why use the Module Pattern?
Â
Why use the Module Pattern?
Â
-
It gives (more) structure
-
Prevent overriding of variable
-
Make code (more) readable
-
Makes multiple JavaScript files easier
-
Anymore?? 🚀
Â
Sources
Â
- JavaScript Module Pattern by Todd Motto
- Everything you wanted to know about JavaScript scope by Todd Motto
Â
JavaScript Module Pattern
By Raymon Schouwenaar
JavaScript Module Pattern
- 1,603