public
Last active

Javascript closures providing private methods and variables.

  • Download Gist
public private javascript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
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'
};
}();
public & private javascript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
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'
};
}();

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.