在 Javascript 中,这个下划线是什么意思?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8288756/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
In Javascript, what does this underscore mean?
提问by TIMEX
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/
我正在阅读有关backbone.js 的教程:http: //addyosmani.com/blog/building-spas-jquerys-best-friends/
What are the underscores? (_index, _photos, _album) Why use them?
下划线是什么?(_index, _photos, _album) 为什么要使用它们?
回答by DrStrangeLove
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.
注意:需要注意的是,仅仅给变量添加下划线并不会使其成为私有变量,这只是一种命名约定。
回答by oli
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
此处对其进行了简要讨论,但建议不要这样做:http: //javascript.crockford.com/code.html
回答by Ryan Gibbons
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.
当使用时,_varname
它只是变量名称的一部分,没有 javascript 含义。开发人员使用它来表示变量的含义或范围。在这种情况下,它看起来像是在告诉开发人员这个变量应该是一个本地或私有变量。
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.
需要注意的一些事情,在这个特定的例子中 using_.varname
将表示具有 underscore.js 库的变量或函数。也可以_varname
用来表示包含下划线对象的变量,类似在我们的办公室,我们$varname
用来表示包含 Jquery 对象的变量。
回答by ThiefMaster
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).
它可能用于标记内部/私有属性。就像在 python 中给变量加上下划线前缀是一种告诉开发人员变量是内部变量的简单方法,他们最好不要篡改它(如果他们这样做,即使是涉及库的小更新也可能会破坏事情)。
回答by Jan Dragsbaek
Usually _
is used to tell the user/programmer that it is a private/protected variable in question.
通常_
用于告诉用户/程序员它是一个私有/受保护的变量。
回答by BeosFreak
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.
如前所述,这是许多开发人员的一种做法,在这方面很糟糕。如果你不得不在你的编程方法中使用这样的约定,那么你应该在尝试使用该语言之前学习该语言、方法和模式。如果有人在不使用“下划线”的情况下无法区分您代码中的公共/私有方法,那么您的文档技能非常缺乏。网络上的许多公共项目都没有很好的文档记录,这可能就是为什么“下划线”约定被大多数受过教育的开发人员“接受”而其他人决定顺其自然而不是保留正式的设计模式和方法的原因。ES6/7 版本中没有写入“下划线”是有原因的。
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.
有一个名为 Doxygen 的免费文档工具。虽然它不特别支持 JavaScript,但当您在评论中使用 Doxygen 前缀时,它可以为您的 JavaScript 应用程序生成专业文档。为开发人员和用户创建带有文档的 JavaScript 应用程序真的很简单,只要您在代码注释中付出一些努力。
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).
请记住,有一些工具可以删除“生产版本”的注释和控制台语句。话虽如此,使用源地图也是浪费时间和资源。在准备发布之前不要缩小.. 即 Dev Build(没有缩小,保留注释和控制台语句),Release Build(删除注释和控制台语句并缩小 Dev build。不需要在发布时重新编译 Dev Build质量代码,只需准备发布并部署它)。
回答by icc97
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.
现在你有两个y.id
和y._id
那个工作并返回相同的值。但是,如果您这样做,console.log(y)
则只会显示_id
密钥。