I had absolutely no idea that it was possible but a couple of weeks ago I discovered that it was possible to create private methods in Javascript.

I really enjoy programming in Javascript and actually far prefer it to several other languages including PHP. I'm always looking for ways to improve my code though and so the ability to reign scope as tightly as possible is more than welcome.

Private methods aren't mentioned in any of the Javascript literature and could arguably be described as a hack but are nonetheless gratefully received.

How?

Javasacript object methods can be defined either in the constructor or via object prototyping and this duality had always confused me. I couldn't figure out what the difference between the two was and why they both existed. It turns out that defining methods in the constructor creates either private or privileged methods and using prototyping creates public methods.

Links

The implementation is not difficult but I'm not even going to touch on it here as I'd just be repeating the excellent tutorial on Douglas Crockford's site.

Crockford also has a number of other very interesting articles on Javascript and points to another big man in the field, Richard Cornford who has a very promising article explaining the concept of javascript closures (which I have still to read).