What's the coding standard for naming a private method in a language which does not have the private modifier? Specifically, I am talking about Javascript. Below I've placed an underscore at the end of my private method's name, but a colleague told me that underscore is usually for class fields.

var MyClass = Class.create({
    initialize: function() {
    publicMethod: function() {
        alert('i am public');
    privateMethod_: function() {
        alert('i am private');
  • Basically this. Python uses double underscore before the function name (it mangles the name, but you can still get to it). Commented Jan 26, 2012 at 0:49
  • Double underscore, you say... What would a single underscore mean?
    – JoJo
    Commented Jan 26, 2012 at 0:56
  • 7
    More detail: Python uses _ for private and __ for Python internal names. You can use _ names, which are simply concealed in a few ways. You should not use __ names because the mangling makes debugging remarkably hard, and it exposes you to the possibility that a future release will interfere with your name. Use _ in Python for "more-or-less private".
    – S.Lott
    Commented Jan 26, 2012 at 0:59
  • @JoJo: Take a look at this, it goes into the details. Commented Jan 26, 2012 at 1:01
  • Be aware that in C and C++, identifiers starting with underscores are reserved to the implementation; what works in one language may not work in another. Commented Jan 26, 2012 at 18:32

2 Answers 2


As everyone told you in a comments, most people(and some languages) use single underscore.


EDIT: Per the comment below by @Raynor, the approach below is not a good way to achieve privacy in JavaScript. See: http://blog.jcoglan.com/2012/01/19/the-cost-of-privacy/

I use this convention:

var MyObject = (function() {
  var initFunction = function() { /* init some stuff */ };
  var privateFunction = function() { /* state secrets */ };

  return {
    init: initFunction

Only the public functions are returned. As you may guess I also tend to use only methods when using this style, no public properties.

  • This style also has the disadvantage of being significantly less efficient then the original style. Generally privacy is never worth run-time penalties.
    – Raynos
    Commented Jan 26, 2012 at 9:23
  • @Raynos, I didn't realize it was so much less efficient. Can you link me to a doc showing this? I'm glad to update my own coding style but I need more data as to why.
    – Ross Smith
    Commented Jan 26, 2012 at 17:04
  • 1
    The cost of privacy mainly about increasing memory usage to O(n) (n === instances) rather then O(1).
    – Raynos
    Commented Jan 26, 2012 at 17:08
  • That was a great read; thanks for sharing!! I'm definitely going to write my next project differently.
    – Ross Smith
    Commented Jan 26, 2012 at 17:28
  • Works great for services or other classes that are only initialized once though. And really, most of your memory is not going to be sucked up by code, but instead data.
    – Domenic
    Commented Jan 26, 2012 at 18:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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