3 ms·
There is actually a much better way to make private functions and properties in JavaScript using the module pattern and closure. Since all functions are object
by benjisg 16y ago
There is actually a much better way to make private functions and properties in JavaScript using the module pattern and closure.
Since all functions are objects in JS, you can create a function with some internal (private) functions and properties than return a object with some publicly accessible functions. A basic example:
https://gist.github.com/866343 https://gist.github.com/866343
Closure gives you a really powerful structure that lets you encapsulate your code and then you don't have to use this and risk clobbering the global window object :)
- Tomek_ 16y agoIt's basically the same thing, closure is the name of the game here. Your solution is probably a bit more costly for memory though.