您现在的位置是:网站首页>软件开发>开发终端>uni-appuni-app

uniapp规范开发(6)编码规范

风口下的猪2020-08-25uni-app

简介

编码规范

优秀的项目源码,即使是多人开发,看代码也如出一人之手。统一的编码规范,可使代码更易于阅读,易于理解,易于维护。尽量按照 ESLint 格式要求编写代码

源码风格

使用 ES6 风格编码

  1. 定义变量使用 let ,定义常量使用 const

  2. 静态字符串一律使用单引号或反引号,动态字符串使用反引号

  // bad
  const a = 'foobar'
  const b = 'foo' + a + 'bar'
 
  // acceptable
  const c = `foobar`
 
  // good
  const a = 'foobar'
  const b = `foo${a}bar`
  const c = 'foobar'

解构赋值

  • 数组成员对变量赋值时,优先使用解构赋值
  // 数组解构赋值
  const arr = [1, 2, 3, 4]
  // bad
  const first = arr[0]
  const second = arr[1]
 
  // good
  const [first, second] = arr
  • 函数的参数如果是对象的成员,优先使用解构赋值
 // 对象解构赋值
  // bad
  function getFullName(user) {
    const firstName = user.firstName
    const lastName = user.lastName
  }
 
  // good
  function getFullName(obj) {
    const { firstName, lastName } = obj
  }
 
  // best
  function getFullName({ firstName, lastName }) {}
  • 拷贝数组,使用扩展运算符(...)拷贝数组。
 const items = [1, 2, 3, 4, 5]
 
  // bad
  const itemsCopy = items
 
  // good
  const itemsCopy = [...items]
  • 箭头函数(需要使用函数表达式的场合,尽量用箭头函数代替。因为这样更简洁,而且绑定了 this)
  // bad
  const self = this;
  const boundMethod = function(...params) {
    return method.apply(self, params);
  }
 
  // acceptable
  const boundMethod = method.bind(this);
 
  // best
  const boundMethod = (...params) => method.apply(this, params);
  • 模块(如果模块只有一个输出值,就使用 export default,如果模块有多个输出值,就不使用 export default,export default 与普通的 export 不要同时使用)
 // bad
  import * as myObject from './importModule'
 
  // good
  import myObject from './importModule'
  • 如果模块默认输出一个函数,函数名的首字母应该小写。
  function makeStyleGuide() {
  }
 
  export default makeStyleGuide;
  • 如果模块默认输出一个对象,对象名的首字母应该大写。
  const StyleGuide = {
    es6: {
    }
  };
 
  export default StyleGuide;




很赞哦! (0)

  • 软件开发
  • 素质要求
  • 计算机基础
  • 架构
  • 安全
  • 性能
  • 运维
  • 尾页
  • 数据库
  • 开发终端
  • 语言基础
  • 项目管理
  • 产品设计
  • 系统
  • 工作规范
  • 计算机网络
  • 前端技术栈
  • 数据结构
  • 计算机组成原理
  • 后端技术栈
  • 性能优化
  • 安全设计
  • 常见模块
  • 计算机操作系统
  • 服务器
  • python
  • MySQL
  • thinkphp
  • PHP
  • Java
  • JavaScript
  • Windows
  • Linux
  • 特效
  • indexedDB
  • vue
  • 淘宝联盟
  • Ionic
  • Angular
  • 微信小程序
  • 支付宝小程序
  • uni-app
  • css/sass/less
  • 支付
  • socket
  • 爬虫
  • web性能优化
  • 消息推送
  • CVM
  • sqlite
  • Redis
  • 前端基础
  • 基础
  • element
  • Nginx
  • yii2
  • /ponder/index.php/index/catelist/catelist/cateid/10.html

    相关阅读 (同一栏目)

    << /

    标签云

    站点信息

    • 文章统计:528篇
    • 移动端访问:扫码进入SQ3R