Hacker News new | ask | show | jobs
by benjisg 5585 days 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

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 :)

1 comments

It's basically the same thing, closure is the name of the game here. Your solution is probably a bit more costly for memory though.