What is the meaning of this underscore in Javascript?

var Gallery = Backbone.Controller.extend({ _index: null, _photos: null, _album :null, _subalbums:null, _subphotos:null, _data:null, _photosview:null, _currentsub:null, routes: { "": "index", "subalbum/:id": "subindex", "subalbum/:id/" : "directphoto", "subalbum/:id/:num" : "hashphoto" }, initialize: function(options) { var ws = this; if (this._index === null){ $.ajax({ url: 'data/album1.json', dataType: 'json', data: {}, success: function(data) { ws._data = data; ws._photos = new PhotoCollection(data); ws._index = new IndexView({model: ws._photos}); Backbone.history.loadUrl(); } }); return this; } return this; }, //Handle rendering the initial view for the //application index: function() { this._index.render(); }, 

I am reading a tutorial on backbone.js here: http://addyosmani.com/blog/building-spas-jquerys-best-friends/

What are underscores? (_index, _photos, _album) Why use them?

+76
javascript oop
Nov 27 '11 at 20:26
source share
7 answers

This means private fields or private methods. Methods that are intended for internal use only.

They should not be called outside the class.

Private fields contain data for internal use.

They should not be read or written (directly) from outside the class.

It is very important to note that simply adding an underscore to a variable does not make it private, it is only a naming convention.

+139
Nov 27 '11 at 20:27
source share

As far as I know, it is usually used to indicate a private variable (but does not actually provide confidentiality, just an agreement).

It is briefly discussed here, although they are advised: http://javascript.crockford.com/code.html

+20
Nov 27 '11 at 20:29
source share

When used as _varname , it is simply part of the variable name and does not matter javascript. Developers use it to indicate the value or scope of a variable. In this case, it looks like he is telling the developer that this variable should be a local or private variable.

A few notes in this particular example using _.varname would mean a variable or function with the underscore.js library. You can also use _varname to denote a variable containing an underscore, similarly in our office, we use $varname to denote a variable containing a jQuery.

+8
Nov 27 '11 at 20:31
source share

It is probably used to indicate internal / private properties. Just like in a python prefix, an underscore variable is an easy way to tell developers that the variable is internal and it’s best not to interfere with it (and if they do, even a minor update to the involved library can break things).

+4
Nov 27 '11 at 20:27
source share

Usually _ used to indicate to the user / programmer that this is the private / protected variable in question.

+2
Nov 27 '11 at 20:28
source share

As already mentioned, this is a practice among many developers, bad at that. If you resort to such conventions in your programming methods, you should learn the language, methods, and patterns before attempting to use the language. If someone cannot distinguish between public / private methods in your code without using underscores, then your documentation skills are sorely lacking. Many public projects on the Internet are very poorly documented, which is probably due to the fact that the “underlining” agreements were “accepted” by most of the educated developers, while others decided to go with the flow, rather than stick to formal templates and design methods. There is a reason why the underscore was not written in ES6 / 7.

In a blog post I recently stumbled upon the Software Developer Manager, who stated: "The underscore naming convention makes it very easy to say, on the one hand, whether the variable function should be public or private." My answer: "Comments are like pictures, in which case they cost thousands of underscores.

There is a free documentation tool called Doxygen. Although it does not support JavaScript, it can create professional documentation for your JavaScript applications when you use the Doxygen prefix in your comments. It is very simple to create JavaScript applications with documentation, both for developers and users, when you put a little effort into the comments of the codes.

Remember that there are tools that can remove comments, and console instructions for Production Releases. Using source maps is a waste of time and resources. Do not reduce until you are ready to publish .. ie Dev Build (without miniaturization, comments and console statements), Release Build (remove comments and console instructions and minimize Dev build. There is no need to recompile Dev Build when it releases high-quality code, just prepare it for release and expand it).

0
Jan 10 '16 at 15:30
source share

This is a small addition. As already answered, these are pseudo-private variables. But then you can write pseudo-public functions that access these private variables.

I got confused in the code of colleagues that really has this (but is buried very deeply in a separate library):

 class x { constructor(id) {this._id = id} get id() {return this._id} } let y = new x(3) 

Now you have y.id and y._id that work and return the same value. But if you execute console.log(y) it only shows the _id key.

enter image description here

0
Dec 13 '18 at 11:52
source share



All Articles