Last active
December 15, 2015 18:09
-
-
Save jbinto/5301893 to your computer and use it in GitHub Desktop.
A practical example of closures in JavaScript: creating custom loggers.
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
function getLogger(prefix) { | |
return function(message) { | |
console.log(prefix + ": " + message); | |
} | |
} | |
// Video component | |
(function() { | |
log = getLogger("video"); | |
log("initialized"); | |
// do some video stuff | |
log("h264 3022kbit/s"); | |
})(); | |
// Audio component | |
(function() { | |
log = getLogger("audio"); | |
log("initialized"); | |
// do some audio stuff | |
log("48khz stereo"); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment