admin管理员组文章数量:1355626
I'm working a lot more with JavaScript in node.js. My application has the following general 'classes' that are used.
Server side:
- Libraries
- Models
- Utilities
- Routes
Client side (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.
The server-side however, gets more messy. For example:
Models are related to MongoDB collections. Each Model is just a wrapper for various functionality. If I have a users collection, I have a collection called users
, my model is Users.js
.
Libraries I have for example Users.js
as well, that interacts with the model and contains the majority of logic.
However, this really shouldn't be called Users
, mainly because I'm getting confused now.
Routes are simply related to the URL. So if you have /account/
I would have an account.js
route - all lowercase.
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.
How would you suggest naming things that are sort of generic like "Libraries", that differentiates them from Models/Routes.
I'm working a lot more with JavaScript in node.js. My application has the following general 'classes' that are used.
Server side:
- Libraries
- Models
- Utilities
- Routes
Client side (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.
The server-side however, gets more messy. For example:
Models are related to MongoDB collections. Each Model is just a wrapper for various functionality. If I have a users collection, I have a collection called users
, my model is Users.js
.
Libraries I have for example Users.js
as well, that interacts with the model and contains the majority of logic.
However, this really shouldn't be called Users
, mainly because I'm getting confused now.
Routes are simply related to the URL. So if you have /account/
I would have an account.js
route - all lowercase.
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.
How would you suggest naming things that are sort of generic like "Libraries", that differentiates them from Models/Routes.
Share Improve this question asked Apr 25, 2012 at 23:21 dzmdzm 23.6k51 gold badges152 silver badges229 bronze badges3 Answers
Reset to default 6The 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.
In some of my node.js projects, I have even taken to not using a .js extension. My routes for instance would be user.routes
. It is easy enough to change the syntax highlighting in editors based on different extensions.
Helpers, extensions, generics, logic, repository, managers, mediator, municator...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. 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 e up with clever names for all of these files when they are all implementing different aspects of a related thing.
本文标签: nodejsJavaScript naming conventions when working with nodejsStack Overflow
版权声明:本文标题:node.js - JavaScript naming conventions when working with nodejs - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744002226a2574070.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论