Created
March 4, 2010 01:03
-
-
Save schell/321279 to your computer and use it in GitHub Desktop.
Javascript closures providing private methods and variables.
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
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' | |
}; | |
}(); |
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
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