Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EN translation for style guide doc #2239

Merged
merged 3 commits into from
Mar 31, 2018
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 63 additions & 1 deletion docs/source/en/style-guide.md
Original file line number Diff line number Diff line change
@@ -1 +1,63 @@
this document is still waiting for translation, see [Chinese Version](/zh-cn/style-guide.html)
## Title: Code Style Guide

Developers are advised to use `egg-init --type=simple showcase` to generate and observe the recommended project structure and configuration.

## Classify

```js
// old style
Module.exports = app => {
Class UserService extends app.Service {
Async list() {
Return await this.ctx.curl('https://eggjs.org');
}
}
Return UserService;
};
```

change to:

```js
Const Service = require('egg').Service;
Class UserService extends Service {
Async list() {
Return await this.ctx.curl('https://eggjs.org');
}
}
Module.exports = UserService;
```

Additionally, the `framework developer` needs to change the syntax as follows, otherwise the `application developer` will have problems customizing base classes such as Service:

```js
Const egg = require('egg');

Module.export = Object.assign(egg, {
Application: class MyApplication extends egg.Application {
// ...
},
// ...
});
```

## Private properties & Lazy Initialization

* Private properties are mounted with `Symbol`.
* The description of Symbol follows the rules of jsdoc, describing the mapped class name + attribute name.
* Delayed initialization.

```js
// app/extend/application.js
Const CACHE = Symbol('Application#cache');
Const CacheManager = require('../../lib/cache_manager');

Module.exports = {
Get cache() {
If (!this[CACHE]) {
This[CACHE] = new CacheManager(this);
}
Return this[CACHE];
},
}
```
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

代码片段首字母大写了,其他没有什么问题~