代码风格指南

title:

建议开发者使用 egg-init --type=simple showcase 来生成并观察推荐的项目结构和配置。

Classify

// old style
module.exports = app => {
  class UserService extends app.Service {
    async list() {
      return await this.ctx.curl('https://eggjs.org');
    }
  }
  return UserService;
};

修改为:

const Service = require('egg').Service;
class UserService extends Service {
  async list() {
    return await this.ctx.curl('https://eggjs.org');
  }
}
module.exports = UserService;

同时,框架开发者需要改变写法如下,否则应用开发者自定义 Service 等基类会有问题:

const egg = require('egg');

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

Private property && Lazy Initialization

  • 私有属性用 Symbol 来挂载。
  • Symbol 的描述遵循 jsdoc 的规则,描述映射后的类名+属性名。
  • 延迟初始化。
// 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];
  },
}