So I have been building this ORM style library for AngularJS (JavaScript) and since I come from a C++/PHP/C# background, I always try to make sure that private things stay private. While this can be accomplished in JavaScript, I keep running into issues that I have to work around probably because JavaScript really does not have the transitional class system that I am used to.

Should I just take the python approach and allow people to access private methods/data if they think they know what they are doing? Is this type of approach common in the JavaScript world?


Use a well-established convention, such as а leading underscore, or a pattern such as the Revealing Module Pattern.

One advantage to using a pattern that enforces visibility at the language level rather than just using a naming convention is intellisense support. Developers that are working in a IDE may not want to see the private methods and data.

The most important thing, though, is that you communicate your intent to other programmers, whether they are working on the library itself or simply using it. This way it is clear which methods and variables are considered implementation details that are subject to change. It will also help you as a developer to minimize your dependencies in the code.

The question you have to decide, then, is whether you want to enforce it at the language level, such that attempting to access it is an error that would require changing the source code to get around. The naming convention is simpler and less strict, but requires more discipline.

  • 1
    I'd say the biggest advantage is that a compiler will bail when you try to access private members. The autocomple
    – tdammers
    Apr 30 '13 at 9:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.