Skip to content

Instantly share code, notes, and snippets.

@Phrogz
Created February 28, 2011 21:59
Show Gist options
  • Save Phrogz/848130 to your computer and use it in GitHub Desktop.
Save Phrogz/848130 to your computer and use it in GitHub Desktop.
Techniques for sharing a variable between two public functions without exposing the variable.
// Technique 1 - local captures
var _xy = (function(){
var shared = {};
var foo = function(){ ...access shared... };
var bar = function(){ ...access shared... };
return [foo,bar];
})();
var foo = _xy[0];
var bar = _xy[1];
// Technique 2 - spam a particular object space
var scope = this; // e.g. global/window
(function(){
var shared = {};
scope.foo = function(){ ...access shared... };
scope.bar = function(){ ...access shared... };
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment