在 Javascript 中,这个下划线是什么意思? [英] In Javascript, what does this underscore mean?

查看:36
本文介绍了在 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();
    },

我正在阅读有关backbone.js 的教程:http://addyosmani.com/blog/building-spas-jquerys-best-friends/

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

下划线是什么?(_index, _photos, _album) 为什么要使用它们?

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

推荐答案

表示私有字段或私有方法.仅供内部使用的方法.

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

不应在类外调用它们.

私有字段包含供内部使用的数据.

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.

这篇关于在 Javascript 中,这个下划线是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆