文件应该被命名为带有连字符的东西。js, camelcase .js,还是别的什么?
我在这里找不到这个问题的答案。
文件应该被命名为带有连字符的东西。js, camelcase .js,还是别的什么?
我在这里找不到这个问题的答案。
当前回答
我不知道javascript文件有什么特别的约定,因为它们在web上与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.
其他回答
JavaScript文件的命名没有官方的、通用的约定。
有一些不同的选择:
scriptName.js script-name.js script_name.js
这些都是有效的命名约定,但我更喜欢jQuery建议的命名约定(对于jQuery插件,尽管它适用于任何JS)
jquery.pluginname.js
这种命名约定的美妙之处在于,它显式地描述了所添加的全局名称空间污染。
Foo.js添加window.foo Foo.bar.js添加window.foo.bar
因为我遗漏了版本:它应该出现在全名之后,最好用连字符分隔,在主版本和次版本之间有句号:
foo-1.2.1.js foo-1.2.2.js ... foo-2.1.24.js
我通常更喜欢小写的连字符,但还有一件事没有提到,那就是有时文件名最好与其中包含的单个模块或可实例化函数的名称完全匹配。
例如,我有一个使用var knockoutUtilityModule = function(){…}在它自己的名为knockoutUtilityModule.js的文件中,尽管客观上我更喜欢knockout-utility-module.js。
类似地,由于我使用捆绑机制来组合脚本,我已经开始在各自的文件中定义可实例化的函数(模板化视图模型等),采用c#风格,以提高可维护性。例如,ProductDescriptorViewModel在ProductDescriptorViewModel.js中独立存在(对于可实例化的函数,我使用大写)。
我不知道javascript文件有什么特别的约定,因为它们在web上与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.
你给出的链接中的问题是关于JavaScript变量的命名,而不是关于文件的命名,所以在你提出问题的环境中忘记它吧。
至于文件命名,这纯粹是一个喜好和品味的问题。我更喜欢用连字符命名文件,因为这样我就不必像处理camelCase文件名时那样用shift键;也因为我不必担心Windows和Linux文件名之间的差异(Windows文件名是不区分大小写的,至少在XP中是这样)。
所以,和许多人一样,答案是“视情况而定”或“由你决定”。
您应该遵循的一个规则是在所选择的约定中保持一致。
一种可能的命名约定是使用类似于jQuery使用的命名方案。它并没有被普遍采用,但相当普遍。
product-name.plugin-ver.sion.filetype.js
product-name + plugin对也可以表示一个命名空间和一个模块。版本和文件类型通常是可选的。
文件类型可以是与文件内容相关的东西。常见的有:
最小化文件 定制用于自定义构建或修改的文件
例子:
jquery-1.4.2.min.js jquery.plugin-0.1.js myapp.invoice.js