简体   繁体   English

使用nodejs时的JavaScript命名约定

[英]JavaScript naming conventions when working with nodejs

I'm working a lot more with JavaScript in node.js. 我在node.js中使用JavaScript工作了很多。 My application has the following general 'classes' that are used. 我的应用程序具有以下使用的一般“类”。

Server side: 服务器端:

  • Libraries 图书馆
  • Models 楷模
  • Utilities 公用事业
  • Routes 路线

Client side (backbone.js): 客户端(backbone.js):

  • Views 查看
  • Models 楷模
  • Collections 集合

The client side is pretty straight forward. 客户端非常直接。 I name all files related to what they are, such as UserModel.js, UserView.js, UserCollection.js, etc. 我将所有与其相关的文件命名为UserModel.js,UserView.js,UserCollection.js等。

The server-side however, gets more messy. 然而,服务器端变得更加混乱。 For example: 例如:

Models are related to MongoDB collections. 模型与MongoDB集合相关。 Each Model is just a wrapper for various functionality. 每个Model只是各种功能的包装器。 If I have a users collection, I have a collection called users , my model is Users.js . 如果我有一个用户集合,我有一个名为users的集合,我的模型是Users.js

Libraries I have for example Users.js as well, that interacts with the model and contains the majority of logic. 我有例如Users.js库,它与模型交互并包含大部分逻辑。

However, this really shouldn't be called Users , mainly because I'm getting confused now. 但是,这真的不应该被称为Users ,主要是因为我现在感到困惑。

Routes are simply related to the URL. 路由只与URL相关。 So if you have /account/ I would have an account.js route - all lowercase. 所以如果你有/account/我会有一个account.js route - 全部小写。

Utilities - I just have one util.js that I don't use much so I'm less concerned and the naming seems fine for it's purpose and size. 实用工具 - 我只有一个util.js,我没有太多使用,所以我不太关心,命名似乎很好,因为它的目的和大小。

How would you suggest naming things that are sort of generic like "Libraries", that differentiates them from Models/Routes. 你会如何建议命名像“库”这样的通用的东西,将它们与模型/路径区分开来。

The most important thing in your naming convention is consistency. 命名约定中最重要的是一致性。 You can figure out pretty much any naming convention as long as it is sane and consistent. 只要它是理智且一致的,你几乎可以找出任何命名约定。

That being said, I would probably be more verbose in my names in this case. 话虽这么说,在这种情况下我的名字可能会更加冗长。 Paths might be good enough, but I would rather see UserRoutes.js , UserModel.js and maybe even UserLib.js based on your examples. 路径可能已经足够好了,但我更UserLib.js根据你的例子看到UserRoutes.jsUserModel.js甚至UserLib.js

In some of my node.js projects, I have even taken to not using a .js extension. 在我的一些node.js项目中,我甚至没有使用.js扩展名。 My routes for instance would be user.routes . 我的路线例如是user.routes It is easy enough to change the syntax highlighting in editors based on different extensions. 很容易根据不同的扩展名更改编辑器中的语法高亮显示。

Helpers, extensions, generics, logic, repository, managers, mediator, communicator...any of those? 助手,扩展,泛型,逻辑,存储库,经理,调解员,沟通者...这些中的任何一个?

I usually name things based on the design pattern I use to implement them. 我通常根据我用来实现它们的设计模式来命名。

I actually do sort of what you're doing now. 我实际上做了你现在正在做的事情。 I have folders for models, routes, views, and controllers. 我有模型,路线,视图和控制器的文件夹。 I then have a file called user.js in each folder. 然后我在每个文件夹中都有一个名为user.js的文件。 When I need to fix a bug or implement something to do with users, it's pretty easy to figure out where I need to go. 当我需要修复错误或实现与用户有关的事情时,很容易找出我需要去的地方。

I think it would be confusing if I tried to come up with clever names for all of these files when they are all implementing different aspects of a related thing. 我认为如果我们试图为所有这些文件提供聪明的名称,而它们都在实现相关事物的不同方面,那将会令人困惑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM