Created
November 23, 2018 06:28
-
-
Save anuraghazra/ab82a09b03c1c78ab1362be14a345a81 to your computer and use it in GitHub Desktop.
Calling a variable inside of a function like a global variable but without polluting the global scope.
This file contains hidden or 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
// Calling a variable inside of a function | |
// like a global variable but without polluting the global scope. | |
// main function | |
function setup() { | |
console.log(ADD); | |
console.log(SUB); | |
console.log(sum(100,50)) | |
} | |
// init the variables in another function (you can hide the function in an external js file) | |
function _setup() { | |
this.ADD = 'ADD'; | |
this.SUB = 'SUB'; | |
this.sum = function (s1, s2) { | |
return s1 + s2; | |
} | |
} | |
// does not work with prototypes | |
// setup.prototype.sub = function(a1,a2) { | |
// return a1-a2; | |
// } | |
// bind the Scope of setup with _setup; | |
setup = setup.bind(_setup(), null); | |
setup(); //finnaly all the setup() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment