js代码风格是怎样的?JavaScript代码风格的介绍

不言
不言原创
2018-08-09 18:00:111069浏览

本篇文章给大家带来的内容是关于js代码风格是怎样的?JavaScript代码风格的介绍,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。

1. 规范

为了约定大家的代码风格,在社区中诞生了一些比较规范的代码风格规范:

  • JavaScript Standard Style

  • Airbnb JavaScript Style

2. ESLint

ESLint 只是提供了工具和规则,如何配置这些规则完全取决于使用者。这里使用 Airbnb 配置来说明。

2.1. 安装

$ npm info "eslint-config-airbnb@latest" peerDependencies
{ eslint: '^4.19.1',  'eslint-plugin-import': '^2.12.0',  'eslint-plugin-jsx-a11y': '^6.0.3',  'eslint-plugin-react': '^7.9.1' }


$ npx -v6.1.0$ npx install-peerdeps --dev eslint-config-airbnb+ eslint-config-airbnb@17.0.0+ eslint-plugin-jsx-a11y@6.1.1+ eslint-plugin-react@7.10.0+ eslint-plugin-import@2.13.0+ eslint@4.19.1SUCCESS eslint-config-airbnb and its peerDeps were installed successfully.

2.2. 配置

# 创建 package.json$ npm init -y

$ eslint --init? How would you like to configure ESLint?
  Answer questions about your style
❯ Use a popular style guide
  Inspect your JavaScript file(s)

? Which style guide do you want to follow?
  Google
❯ Airbnb
  Standard

? Do you use React? No
? What format do you want your config file to be in? JSON

+ eslint-plugin-import@2.13.0+ eslint-config-airbnb-base@13.0.0

最后生成 .eslint.json 文件:

{
    "extends": "airbnb-base"}

2.3. 使用

2.3.1. 在命令行中

$ ./node_modules/.bin/eslint -v
v4.19.1$ ./node_modules/.bin/eslint ./blog//Users/forwardNow/develop/work/study/blog/2018/08/test.js  35:1  warning  Unexpected console statement  no-console✖ 1 problem (0 errors, 1 warning)

2.3.2. 配置 npm script

"scripts": {
  "eslint": "./node_modules/.bin/eslint ./src/"},

2.3.3. 在 vscode 中

搜索并安装 eslint 插件

配置 User Settings:

"editor.detectIndentation": false,"editor.tabSize": 2,"eslint.autoFixOnSave": true,

以上就是js代码风格是怎样的?JavaScript代码风格的介绍的详细内容,更多请关注php中文网其它相关文章!

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。