• 技术文章 >web前端 >js教程

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

    不言不言2018-08-09 18:00:11原创648
    本篇文章给大家带来的内容是关于js代码风格是怎样的?JavaScript代码风格的介绍,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。

    1. 规范

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

    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中文网其它相关文章!

    声明:本文原创发布php中文网,转载请注明出处,感谢您的尊重!如有疑问,请联系admin@php.cn处理
    专题推荐:eslint
    上一篇:js实现图片轮播的代码实例(详细代码) 下一篇:js删除操作delete步骤详解
    大前端线上培训班

    相关文章推荐

    • innerhtml是jquery方法么• javascript 怎么将时间转毫秒• javascript怎么设置标签的背景颜色• jquery select 不可编辑怎么办• javascript怎么判断指定值是否是偶数

    全部评论我要评论

  • 取消发布评论发送
  • 1/1

    PHP中文网