在Javascript中,这个下划线是什么意思?

发布于 2024-12-18 12:33:09 字数 1359 浏览 0 评论 0原文

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/

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

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'm reading a tutorial on backbone.js here: http://addyosmani.com/blog/building-spas-jquerys-best-friends/

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

土豪 2024-12-25 12:33:09

它意味着私有字段或私有方法。仅供内部使用的方法。

不应该在类之外调用它们。

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

不应从类外部(直接)读取或写入它们。

注意:需要注意的是,仅向变量添加下划线并不能使其成为私有变量,这只是一种命名约定。

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.

我不在是我 2024-12-25 12:33:09

据我所知,它通常用于指示私有变量(但实际上并不提供任何隐私,只是一种约定)。

此处对其进行了简要讨论,但建议不要这样做:
http://javascript.crockford.com/code.html

As far as I'm aware, it's generally used to indicate a private variable (but doesn't actually provide any privacy, just a convention).

It's discussed briefly here, though they're advised against:
http://javascript.crockford.com/code.html

向地狱狂奔 2024-12-25 12:33:09

当像 _varname 使用时,它只是变量名称的一部分,没有 JavaScript 含义。开发人员使用它来表示变量的含义或范围。在这种情况下,它看起来像是在告诉开发人员这个变量应该是本地变量或私有变量。

有几点需要注意,在这个特定的示例中,使用 _.varname 将表示带有 underscore.js 库的变量或函数。也可以使用 _varname 来表示包含下划线对象的变量,类似地在我们的办公室,我们使用 $varname 来表示包含 Jquery 对象的变量。

When used like _varname it's just part of the variables name, and has no javascript meaning. Developers use it to signify the meaning or scope of the variable. In this case it looks like it is telling the developer this variable should be a local or private variable.

A few things to note, in this particular example using _.varname would signify a variable or function with the underscore.js library. Also one could use _varname to signify a variable holding an underscore object, similarly at our office, we use $varname to signify a variable containing a Jquery object.

假装爱人 2024-12-25 12:33:09

它可能用于标记内部/私有属性。就像在 python 中一样,在变量前添加下划线是一种简单的方法,可以告诉开发人员该变量是内部变量,他们最好不要篡改它(如果他们这样做,即使对所涉及的库进行微小的更新也可能会破坏一些东西)。

It's probably used to mark internal/private properties. Just like in python prefixing a variable with a underscore is an easy way to tell developers that a variable is internal and they better not tamper with it (and if they do so, even a minor update of the involved library may break things).

送舟行 2024-12-25 12:33:09

通常 _ 用于告诉用户/程序员它是一个私有/受保护的变量。

Usually _ is used to tell the user/programmer that it is a private/protected variable in question.

清风夜微凉 2024-12-25 12:33:09

这是一个小小的补充。正如已经回答的那样,这些是伪私有变量。但是可以编写访问这些私有变量的伪公共函数。

我对同事的代码感到困惑,该代码有效地具有此功能(但埋藏在一个单独的库中很深):

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

现在您拥有可以工作的 y.id 和 y._id 了返回相同的值。但如果您执行 console.log(y) 它只会显示 _id 键。

输入图片此处描述

This is a slight addendum. As already answered these are pseudo private variables. But it is then possible to write pseudo public functions that access these private variables.

I got confused by a colleagues code that effectively has this (but buried very deep in a separate library):

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

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

enter image description here

仄言 2024-12-25 12:33:09

如前所述,这是许多开发人员的一种做法,但这种做法很糟糕。如果您必须在编程方法中采用这样的约定,那么您应该在尝试使用该语言之前学习该语言、方法和模式。如果有人在不使用“下划线”的情况下无法区分代码中的公共/私有方法,那么你的文档技能就非常缺乏。网络上的许多公共项目的文档记录都很差,这可能就是为什么“下划线”约定被大多数受过教育的开发人员“接受”,而其他开发人员则决定随波逐流而不是保留正式的设计模式和方法。 ES6/7版本中没有写入“下划线”是有原因的。

在一篇博客中,我最近遇到一位软件工程师经理,他说:“下划线命名约定让我们很容易一眼就能看出变量函数是公共的还是私有的。” 。我的回答是:“注释就像图片,在这种情况下它们相当于一千个下划线。

有一个免费的文档工具,称为 Doxygen。虽然它不专门支持 JavaScript,但当您使用 Doxygen 时,它可以为您的 JavaScript 应用程序生成专业文档当您在代码注释中投入一点精力时,创建带有文档的 JavaScript 应用程序非常简单。

请记住,有一些工具可以删除注释和“生产版本”的控制台语句。那个存在说,使用源映射也是浪费时间和资源,在准备发布之前也不要缩小。即开发构建(不缩小,保留注释和控制台语句),发布构建(删除注释和控制台语句)。并缩小开发版本。在发布质量代码时无需重新编译开发版本,只需准备发布并部署即可)。

As mentioned, it's a practice among many developers, a bad one at that. If you have to resort to conventions like this in your programming methods then you should learn the language, methods and patterns before attempting to use the language. If someone can't distinguish between public/private methods in your code with out the use of the "underscore", then your documentation skill are extremely lacking. Many of the public projects on the web are very poorly documented which is probably why the "underscore" conventions was "accepted" by most under educated developers while others decided to go with the flow rather than keeping the formal design patterns and methods. There is a reason why "underscore" was not written into ES6/7 versions.

In a blog I recently came across an Software Engineer Manager who stated: "The underscore naming convention makes it really easy to tell, at a glance, whether a variable function is intended to be public or private.". My response is: "Comments are like pictures, in this case they are worth a thousand underscores.

There is a free documentation tool called Doxygen. While it does not specifically support JavaScript, it can generate professional documentation for your JavaScript applications when you use Doxygen prefix's in your comments. It's really simple to create JavaScript applications with documentation, for both developers and users when you put a little effort into your code comments.

Remember, there are tools that can remove comments, and console statements for "Production Releases". That being said, the use of source maps are a waste of time and resources also. Don't minify until your ready to publish.. i.e. Dev Build (no minification, keep comments and console statements), Release Build (remove comments, and console statements and minify the Dev build. No need to recompile the Dev Build when its release quality code, just prepare it for release and deploy it).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文