Object-oriented – Why is it uncommon (is it?) to use _ (underscore) in JS private vars

coding-standardsjavascriptnotationobject-oriented

Coming to JavaScript from C# and C/C++ world, I'm used to decorating my private members with underscore.

But a couple of JS devs I know have told me it's not common in JS world, and my code looks "weird" to them. I got intrigued and interviewed about 4-5 more JS devs and they all confirmed.

Just to be clear, her's my code:

var myObject = new myClass("some value");

function myClass(param) {
    var _privateVar = param;

    this.publicMethod = function() {
        alert(_privateVar);
    }
}

Now my questions are:

  1. Is this true? Should I stop writing code like this – note, I ask only in the scope of hiring, just wanna be sure that newcomers won't find our existing code "weird"

  2. If it is true, is there any explanation? Or it sorta "historically" happened? Not trying to start a flame-war or anything, just curious, I'm fine with dumping my old habits.

UPDATE

OK, maybe I shouldn't call it "private" variable b/c it's actually a "local" variable… But you know what I mean. I need to be able to quickly distinguish variable's scope, by simply looking at it. How "local" are the vars? Are they local to publicMethod or to the whole myClass? That's the reason of using underscore, not the actual "privacy"

Best Answer

Perhaps it's not as common as you might expect because javascript doesn't exactly have private variables...

The use of the _ prefix is something that non-javascript developers bring in from other languages, but it's not conventional and might cause some confusion.

It's not altogether unheard of to use the _ prefix, though, as you can see in other SO questions such as:

https://stackoverflow.com/questions/34418012/how-to-work-with-private-variables-in-es6

https://stackoverflow.com/questions/22156326/private-properties-in-javascript-es6-classes

or documentation such as

https://developer.mozilla.org/en-US/Add-ons/SDK/Guides/Contributor_s_Guide/Private_Properties

There are also some very well respected developers who advocate for using the _, an example of which is this comment by Ward Bell:

JavaScript doesn't have true private variables and the use of classes means that we have even less of an ability to hide variables than in pre-class JavaScript. We have only the '_' convention to save us.

The primary value of the '_' convention is that it shouts "Do not touch!". When debugging the JavaScript it's all we have to go on.

When people see something.foo in the debugger, they can't know for certain if that foo is (a) public and undocumented or (b) private. Without adequate notice, they feel free to reference it in their own code. In that moment, the developer incurs a support obligation.

When readers see _foo, they should know it is off limits. If they reference _foo they do so at their own risk. There must be no tears when the developer removes it or changes its behavior.

In our docs and samples private members should begin with ''. Where we have neglected to prefix a private with '', we correct that quickly.

Related Topic