I've been playing around with a pattern in javascript that would enable private/protected members. While there are many patterns that enable this all come with a different set of forces. Here are some:
Crockford's private members in javascript
But I was looking for a pattern that could use prototypical inheritence, where private function could be shared across different instances (unlike Crockfords pattern). I then stumbled over this post by someone named T.J Crowder. In this post mention the specification draft of ECMAScript 6 and the proposal to introduce 'private name object'. He goes on to implement something similar in current versions of javascript.
I've experimented with a tiny helper 'ClassFactory.js' that help you define both private and protected members in a short and consice manner. It uses T.J's implementation to make it possible.
With this helper you would define a class like this (the sample mimics some Backbone view functionallity):
There is nothing fishy going on here, ClassFactory.create() will just return the constructor after adding all the members to the prototype. The private/protected members will have random names so that they can't be used by any other objects. The namespace (ns) keeps a reference to that name. When/if private name object is implemented then the random string can be removed and exchanged for the name object instead.
Check out the rest of the gist to se how inheritence works with ClassFactory.