什么是 javascript 文件名命名约定?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7273316/
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
What is the javascript filename naming convention?
提问by ripper234
采纳答案by Bryan Menard
One possible naming convention is to use something similar to the naming scheme jQuery uses. It's not universally adopted but it is pretty common.
一种可能的命名约定是使用类似于 jQuery 使用的命名方案的东西。它没有被普遍采用,但很常见。
product-name.plugin-ver.sion.filetype.js
where the product-name
+ plugin
pair can also represent a namespaceand a module. The version
and filetype
are usually optional.
其中product-name
+plugin
对还可以表示命名空间和模块。在version
与filetype
通常是可选的。
filetype
can be something relative to how the content of the file is. Often seen are:
filetype
可以是与文件内容有关的东西。经常看到的是:
min
for minified filescustom
for custom built or modified files
min
对于缩小的文件custom
用于自定义构建或修改的文件
Examples:
例子:
jquery-1.4.2.min.js
jquery.plugin-0.1.js
myapp.invoice.js
jquery-1.4.2.min.js
jquery.plugin-0.1.js
myapp.invoice.js
回答by jfriend00
I'm not aware of any particular convention for javascript files as they aren't really unique on the web versus css files or html files or any other type of file like that. There are some "safe" things you can do that make it less likely you will accidentally run into a cross platform issue:
我不知道 javascript 文件的任何特定约定,因为与 css 文件或 html 文件或任何其他类型的文件相比,它们在网络上并不是真正独特的。您可以做一些“安全”的事情,以减少意外遇到跨平台问题的可能性:
- Use all lowercase filenames.There are some operating systems that are not case sensitive for filenames and using all lowercase prevents inadvertently using two files that differ only in case that might not work on some operating systems.
- Don't use spaces in the filename.While this technically can be made to work there are lots of reasons why spaces in filenames can lead to problems.
- A hyphen is OK for a word separator.If you want to use some sort of separator for multiple words instead of a space or camelcase as in
various-scripts.js
, a hyphen is a safe and useful and commonly used separator. - Think about using version numbers in your filenames.When you want to upgrade your scripts, plan for the effects of browser or CDN caching. The simplest way to use long term caching (for speed and efficiency), but immediate and safe upgrades when you upgrade a JS file is to include a version number in the deployed filename or path (like jQuery does with jquery-1.6.2.js) and then you bump/change that version number whenever you upgrade/change the file. This will guarantee that no page that requests the newer version is ever served the older version from a cache.
- 使用所有小写文件名。有些操作系统对文件名不区分大小写,使用全部小写字母可防止无意中使用两个不同的文件,以防万一可能无法在某些操作系统上运行。
- 不要在文件名中使用空格。虽然这在技术上是可行的,但文件名中的空格会导致问题的原因有很多。
- 连字符可以用作单词分隔符。如果您想对多个单词使用某种分隔符而不是空格或驼峰式大小写
various-scripts.js
,那么连字符是一种安全且有用且常用的分隔符。 - 考虑在文件名中使用版本号。当您想要升级您的脚本时,请计划浏览器或 CDN 缓存的影响。使用长期缓存(为了速度和效率)的最简单方法,但在升级 JS 文件时立即安全的升级是在部署的文件名或路径中包含版本号(就像 jQuery 对 jquery-1.6.2.js 所做的那样) 然后在升级/更改文件时更改/更改该版本号。这将保证不会从缓存中为请求较新版本的页面提供较旧版本。
回答by zzzzBov
There is no official, universal, convention for naming JavaScript files.
没有官方的、通用的、命名 JavaScript 文件的约定。
There are some various options:
有一些不同的选择:
scriptName.js
script-name.js
script_name.js
scriptName.js
script-name.js
script_name.js
are all valid naming conventions, however Iprefer the jQuery suggested naming convention (for jQuery plugins, although it works for any JS)
都是有效的命名约定,但是我更喜欢 jQuery 建议的命名约定(对于 jQuery 插件,尽管它适用于任何 JS)
jquery.pluginname.js
jquery.pluginname.js
The beauty to this naming convention is that it explicitlydescribes the global namespace pollution being added.
这种命名约定的美妙之处在于它明确描述了正在添加的全局命名空间污染。
foo.js
addswindow.foo
foo.bar.js
addswindow.foo.bar
foo.js
添加window.foo
foo.bar.js
添加window.foo.bar
Because I left out versioning: it should come after the full name, preferably separated by a hyphen, with periods between major and minor versions:
因为我省略了版本控制:它应该在全名之后,最好用连字符分隔,主要版本和次要版本之间有句点:
foo-1.2.1.js
foo-1.2.2.js
- ...
foo-2.1.24.js
foo-1.2.1.js
foo-1.2.2.js
- ...
foo-2.1.24.js
回答by Pete Wilson
The question in the link you gave talks about naming of JavaScript variables, not about file naming, so forget about that for the context in which you ask your question.
您提供的链接中的问题讨论的是 JavaScript 变量的命名,而不是文件命名,因此请忽略您提出问题的上下文。
As to file naming, it is purely a matter of preference and taste. I prefer naming files with hyphens because then I don't have to reach for the shift key, as I do when dealing with camelCase file names; and because I don't have to worry about differences between Windows and Linux file names (Windows file names are case-insensitive, at least through XP).
至于文件命名,纯粹是喜好和品味的问题。我更喜欢用连字符命名文件,因为这样我就不必像处理驼峰式文件名时那样使用 shift 键;并且因为我不必担心 Windows 和 Linux 文件名之间的差异(Windows 文件名不区分大小写,至少通过 XP)。
So the answer, like so many, is "it depends" or "it's up to you."
因此,与许多人一样,答案是“这取决于”或“取决于您”。
The one rule you should follow is to be consistent in the convention you choose.
您应该遵循的一项规则是在您选择的约定中保持一致。
回答by Tom W Hall
I generally prefer hyphens with lower case, but one thing not yet mentioned is that sometimes it's nice to have the file name exactly match the name of a single module or instantiable function contained within.
我通常更喜欢小写的连字符,但尚未提到的一件事是,有时让文件名与包含在其中的单个模块或可实例化函数的名称完全匹配是很好的。
For example, I have a revealing module declared with var knockoutUtilityModule = function() {...}
within its own file named knockoutUtilityModule.js, although objectively I prefer knockout-utility-module.js.
例如,我var knockoutUtilityModule = function() {...}
在自己的文件中声明了一个显性模块,名为knockoutUtilityModule.js,尽管客观上我更喜欢knockout-utility-module.js。
Similarly, since I'm using a bundling mechanism to combine scripts, I've taken to defining instantiable functions (templated view models etc) each in their own file, C# style, for maintainability. For example, ProductDescriptorViewModel lives on its own inside ProductDescriptorViewModel.js (I use upper case for instantiable functions).
同样,由于我使用捆绑机制来组合脚本,因此为了可维护性,我已经开始在各自的 C# 样式文件中定义可实例化的函数(模板化视图模型等)。例如,ProductDescriptorViewModel 独立存在于 ProductDescriptorViewModel.js 中(我使用大写表示可实例化的函数)。