Skip to content

Commit

Permalink
docs: EN translation for style guide doc (#2239)
Browse files Browse the repository at this point in the history
  • Loading branch information
cemremengu authored and popomore committed Mar 31, 2018
1 parent d9c4ec2 commit 348ff18
Showing 1 changed file with 63 additions and 1 deletion.
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.exports = 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];
}
};
```

0 comments on commit 348ff18

Please sign in to comment.