Skip to content

In Javascript, what does this underscore mean?

var Gallery = Backbone.Controller.extend({
    _index: null,
    _photos: null,
    _album :null,
    routes: {
        "": "index",
        "subalbum/:id": "subindex",
        "subalbum/:id/" : "directphoto",
        "subalbum/:id/:num" : "hashphoto"
    initialize: function(options) {
        var ws = this;
        if (this._index === null){
                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});
            return this;
        return this;
    //Handle rendering the initial view for the
    index: function() {

I’m reading a tutorial on backbone.js here:

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



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

They should not be invoked outside of the class.

Private fields contain data for internal use.

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

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

User contributions licensed under: CC BY-SA
5 People found this is helpful