Last active
July 10, 2018 05:58
-
-
Save tarun-nagpal-github/92da292e1fa453cd5dfd603c1c6dc2ef to your computer and use it in GitHub Desktop.
Revealing Module Design Pattern
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* | |
* @param {number} x | |
* @param {number} y | |
* | |
* @returns function | |
*/ | |
var Calculator = (function(){ | |
var _privateVariable = 100; | |
//Private function | |
function _privateFunction(x, y){ | |
return (x*x) + (y *y); | |
} | |
function sum(x, y){ | |
return x + y; | |
} | |
function mul(x, y){ | |
return x * y; | |
} | |
function divide(x, y){ | |
return x / y; | |
} | |
function square(x, y) { | |
return _privateFunction(x, y); | |
} | |
// functions are revelied here | |
return { | |
sum : sum, | |
mul : mul, | |
divide : divide, | |
square: square | |
} | |
})(); | |
var cal = Calculator; | |
console.log(cal.sum(2,3)); | |
console.log(cal.mul(2,3)); | |
console.log(cal.divide(2,3)); | |
console.log(cal.square(2,3)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment