-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: EN translation for style guide doc (#2239)
- Loading branch information
1 parent
d9c4ec2
commit 348ff18
Showing
1 changed file
with
63 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; | ||
} | ||
}; | ||
``` |