Skip to content

Latest commit

 

History

History
130 lines (104 loc) · 1.87 KB

代码风格.md

File metadata and controls

130 lines (104 loc) · 1.87 KB

Node.js进阶 代码风格

⭐ by calidion


什么是代码风格?

代码风格是指写代码时所遵从的规范。

行业与企业都会有不同的规范对程序开发人员进行约束。

通常这些约束具有一定的实际价值,程序开发人员应该遵从这些约束。


类别

所以通常这些代码风格规范分成两类:

一类是lint,用于相对粗糙的检测代码基本的格式。

另一类是企业规范,在lint基础上,对详细的编码细节进行约束


lint工具

目前常用的风格工具有:

  1. eslint
  2. jshint
  3. jslint

企业风格

常用的企业风格规范有:

  1. airbnb
  2. google
  3. standard
  4. nodejs

ESLint使用

  1. 安装
npm i eslint
  1. 初始化eslint参数
./node_modules/.bin/eslint --init
  1. 调用
./node_modules/.bin/eslint .

  1. 尝试修复
./node_modules/.bin/eslint . --fix

操作步骤

  1. clone你自己fork出来的库
git clone https://github.com/<yourname>/homeworks.git
  1. 进入目录
cd homeworks
  1. 更新master数据
git pull https://github.com/<yourname>/homeworks.git

  1. 安装eslint
npm i <eslint>
  1. 初始化你的目
./node_modules/.bin/eslint <yourdir>

no-console问题的修改

  1. 通过debug包替换它
var debug = require('debug')('xxx');
  1. console.log
debug("log:" + info)
  1. console.warn
debug("warn:" + info)

  1. console.error
debug("error:" + info)

查看这个DEBUG信息

DEBUG=xxx node your.js