什么是javascript文件名命名约定?

时间:2022-01-21 23:52:40

Should files be named something-with-hyphens.js, camelCased.js, or something else?

文件应该被命名为-连字符。js,camelCased。js或别的东西吗?

I didn't find the answer to this question here.

我没有找到这个问题的答案。

5 个解决方案

#1


139  

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 namespace and a module. The version and filetype are usually optional.

产品名称+插件对还可以表示名称空间和模块。版本和文件类型通常是可选的。

filetype can be something relative to how the content of the file is. Often seen are:

filetype可以是与文件内容相关的内容。经常看到有:

  • min for minified files
  • 分钟缩小的文件
  • custom for custom built or modified files
  • 自定义构建或修改的文件

Examples:

例子:

  • jquery-1.4.2.min.js
  • jquery-1.4.2.min.js
  • jquery.plugin-0.1.js
  • jquery.plugin-0.1.js
  • myapp.invoice.js
  • myapp.invoice.js

#2


90  

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文件的任何特定约定,因为它们在web上并不是唯一的,与css文件或html文件或任何其他类型的文件相比。你可以做一些“安全”的事情,这样你就不太可能偶然遇到跨平台问题:

  1. 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.
  2. 使用所有小写文件名。有一些操作系统对文件名不区分大小写,并且使用所有小写都可以避免在无意中使用两个文件,这些文件只在某些操作系统上可能无法使用时不同。
  3. 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.
  4. 不要在文件名中使用空格。虽然这在技术上是可行的,但是文件名中的空格可能导致问题的原因有很多。
  5. 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.
  6. 连字符对于单词分隔符来说是可以的。如果您想要对多个单词使用某种分隔符,而不是在不同的脚本中使用空格或camelcase。连字符是一种安全、有用、常用的分隔符。
  7. 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.
  8. 考虑在文件名中使用版本号。当您想要升级脚本时,请考虑浏览器或CDN缓存的影响。最简单的方法使用长期缓存(速度和效率),但直接和安全升级,当你升级一个JS文件包含版本号在已部署的文件名或路径(像jQuery jquery-1.6.2.js)然后你撞/当你改变版本号升级/修改文件。这将保证从缓存中不会为请求更新版本的页面提供旧版本。

#3


47  

There is no official, universal, convention for naming JavaScript files.

没有正式的、通用的、命名JavaScript文件的约定。

There are some various options:

有一些不同的选择:

  • scriptName.js
  • scriptName.js
  • script-name.js
  • script-name.js
  • script_name.js
  • script_name.js

are all valid naming conventions, however I prefer 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 explicitly describes the global namespace pollution being added.

这种命名约定的好处在于它显式地描述了添加的全局名称空间污染。

  • foo.js adds window.foo
  • foo。js window.foo补充道
  • foo.bar.js adds window.foo.bar
  • 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.1.js
  • foo-1.2.2.js
  • foo-1.2.2.js
  • ...
  • foo-2.1.24.js
  • foo-2.1.24.js

#4


12  

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).

至于文件命名,这纯粹是一个偏好和品位的问题。我更喜欢用连字符命名文件,因为这样我就不需要像处理camelCase文件名那样访问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.

您应该遵循的一个规则是在您选择的约定中保持一致。

#5


6  

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的文件中。虽然客观上我更喜欢淘汰制-模块。

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(我用大写的实例来表示函数)。

#1


139  

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 namespace and a module. The version and filetype are usually optional.

产品名称+插件对还可以表示名称空间和模块。版本和文件类型通常是可选的。

filetype can be something relative to how the content of the file is. Often seen are:

filetype可以是与文件内容相关的内容。经常看到有:

  • min for minified files
  • 分钟缩小的文件
  • custom for custom built or modified files
  • 自定义构建或修改的文件

Examples:

例子:

  • jquery-1.4.2.min.js
  • jquery-1.4.2.min.js
  • jquery.plugin-0.1.js
  • jquery.plugin-0.1.js
  • myapp.invoice.js
  • myapp.invoice.js

#2


90  

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文件的任何特定约定,因为它们在web上并不是唯一的,与css文件或html文件或任何其他类型的文件相比。你可以做一些“安全”的事情,这样你就不太可能偶然遇到跨平台问题:

  1. 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.
  2. 使用所有小写文件名。有一些操作系统对文件名不区分大小写,并且使用所有小写都可以避免在无意中使用两个文件,这些文件只在某些操作系统上可能无法使用时不同。
  3. 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.
  4. 不要在文件名中使用空格。虽然这在技术上是可行的,但是文件名中的空格可能导致问题的原因有很多。
  5. 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.
  6. 连字符对于单词分隔符来说是可以的。如果您想要对多个单词使用某种分隔符,而不是在不同的脚本中使用空格或camelcase。连字符是一种安全、有用、常用的分隔符。
  7. 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.
  8. 考虑在文件名中使用版本号。当您想要升级脚本时,请考虑浏览器或CDN缓存的影响。最简单的方法使用长期缓存(速度和效率),但直接和安全升级,当你升级一个JS文件包含版本号在已部署的文件名或路径(像jQuery jquery-1.6.2.js)然后你撞/当你改变版本号升级/修改文件。这将保证从缓存中不会为请求更新版本的页面提供旧版本。

#3


47  

There is no official, universal, convention for naming JavaScript files.

没有正式的、通用的、命名JavaScript文件的约定。

There are some various options:

有一些不同的选择:

  • scriptName.js
  • scriptName.js
  • script-name.js
  • script-name.js
  • script_name.js
  • script_name.js

are all valid naming conventions, however I prefer 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 explicitly describes the global namespace pollution being added.

这种命名约定的好处在于它显式地描述了添加的全局名称空间污染。

  • foo.js adds window.foo
  • foo。js window.foo补充道
  • foo.bar.js adds window.foo.bar
  • 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.1.js
  • foo-1.2.2.js
  • foo-1.2.2.js
  • ...
  • foo-2.1.24.js
  • foo-2.1.24.js

#4


12  

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).

至于文件命名,这纯粹是一个偏好和品位的问题。我更喜欢用连字符命名文件,因为这样我就不需要像处理camelCase文件名那样访问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.

您应该遵循的一个规则是在您选择的约定中保持一致。

#5


6  

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的文件中。虽然客观上我更喜欢淘汰制-模块。

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(我用大写的实例来表示函数)。