Web 开发的命名标准,例如。表单字段、CSS 类、JavaScript 变量

发布于 2024-10-03 12:40:23 字数 501 浏览 6 评论 0原文

我在一家拥有多个网站的公司工作,所有网站都以某种方式共享某种通用代码。最近我正在开发一个项目来改造特定网站的 UI,以下是我遇到的一些问题:

  1. 我开始使用“Web 部件”概念通过 ajax 从不同页面提取内容。但是我有名字/ID 冲突。例如。主页有一个标签

    ,我拉出的 Web 部件也有一个标签

  2. 有不止 1 个人在编写 javascript,有时编写库代码,有时编写特定于页面的代码。而且我们存在命名冲突...

我正在考虑将命名约定标准化为这样:全局变量将具有“global_”前缀,页面特定代码应具有“[pagename]_”前缀(但是,我们会看到很多index.html 的冲突)

因此我想问是否有人有一个相当可靠的标准化可以分享?

提前致谢。

詹姆斯

I work for a company which has multiple websites, all of sites somehow share some kind of common code. Recently I am working on a project to revamp the UI of a particular website, here are some the problems I have faced:

  1. I started to "web parts" concept to pull content from different page via ajax. However I have name/id clashes. Eg. the main page has a tag <div id="tmp">, and the web part that I am pulling over also has a tag <div id="tmp">

  2. There are more than 1 guy writing javascripts, sometimes writing library code, sometimes writing page specific code. And we have naming clashes...

I was thinking to standardize the naming convention to something like this: global variables will have "global_" prefixes, page specific code should have "[pagename]_" prefixes(however, we will see a lot of clashes for index.html)

Hence I am asking if anyone has a pretty solid standardization to share with?

Thanks in advance.

James

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

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

发布评论

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

评论(1

顾挽 2024-10-10 12:40:23

我建议,如果您从头开始,那么将所有全局内容放入一个名称空间中,就像 jQuery 使用 $ 符号那样,将是一个好主意。

这样,个人开发人员就知道他们不必担心隐藏全局变量,反之亦然的全局代码。

您实质上是建议使用您的名称前缀为全局代码和页面代码提供单独的名称空间。然而,这很快就会变得令人恼火并且难以改变。

您甚至可以按照您关于变量命名方案的建议为每个页面提供名称空间。

I would suggest that if you were starting for scratch then putting all of your global stuff into a single name space like jQuery do with their $ symbol would be a good idea.

That way individual developers know they don't have to worry about hiding a global variable and visa versa for the global code.

You were essentially suggesting providing separate name spaces for global and page code with your name prefixes. However that would quickly become irritating and hard to modify.

You could even have name spaces for each page as you suggested regarding the variable naming scheme.

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