-
-
Save codefuncode/6668111453245ffe3f5c9ec8c2ebe994 to your computer and use it in GitHub Desktop.
JS Closure Snippet
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
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
<title>Document</title> | |
</head> | |
<body> | |
<script lang="javascript"> | |
var module = function(){ | |
var theArray = []; | |
return{ | |
loader : function(){ | |
theArray[0] = function() { console.log("first function"); } | |
theArray[1] = function() { console.log("second function"); } | |
theArray[2] = function() { console.log("third function"); } | |
}, | |
getData : function(id){ | |
theArray[id](); | |
} | |
} | |
}(); | |
module.loader(); | |
module.getData(0); | |
module.getData(1); | |
module.getData(2); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment