Skip to content

Instantly share code, notes, and snippets.

@schell
Created March 4, 2010 01:03
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save schell/321279 to your computer and use it in GitHub Desktop.
Save schell/321279 to your computer and use it in GitHub Desktop.
Javascript closures providing private methods and variables.
var object = function() {
var _privateVariable = 0;
function _privateFunction() {
alert('i am in a closure');
}
function _privateFunctionTwo() {
alert('nothing outside the object created by this function can reach me.');
_privateFunction();
}
return {
publicFunction : function() {
alert('everything can reach me, and i can call private functions in this closure...');
_privateFunctionTwo();
_privateVariable++;
},
publicProperty : 'i am public property'
};
}();
var object = function() {
function privateFunction() {
alert('i am in a closure');
}
function privateFunctionTwo() {
alert('nothing outside the object created by this function can reach me.');
}
return {
publicFunction : function() {
alert('everything can reach me');
},
publicProperty : 'i am public property'
};
}();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment