代码规范

  1. 参数 >= 3 时,最好封装在对象中传递才更优雅。

  2. 关于页面中异步函数调用,最好使用async/await。

    1. 标签上的行为操作,简单写

          // 创建 or 更新
          async saveHandler() {
              const data = this.state;
              if (data.status) {
                  this.showToast('在线状态不允许更改数据,如需修改,请先下线该配置');
                  return;
              }
              const canSubmit = this.validata();
              if (!canSubmit) {
                  return;
              }
              const configs = this.formatData();
              const self = this;
              try {
                  const self = this;
                  const type = state.get('isEditPage') ? 'update' : 'create';
                  const response = await ajax.post('/api/new/resource/configRule/create').send(Object.assign(data, { configs, businessKey, type }));
                  if (!response) {
                      throw new Error('网络异常,请稍后重试');
                  }
                  const res = response.body || {};
                  if (res.status !== 0) {
                      if (res.error && res.error.message) {
                          throw new Error(res.error.message);
                      }
                      throw new Error('网络异常,请稍后重试');
                  }
                  self.showToast('创建成功', 3000);
                  window.location.href = `/resource/config/list/${businessKey}`;
              } catch (e) {
                  self.showToast(e.toString(), 3000);
              }
          }
      
    1. 方法之间的调用

      const logger = (...args) => {
        console.log(`[${new Date()}]`, ...args);
      }
      
      async function myAsyncFunc() {
        function promiseHelper() {
          return new Promise(resolve => setTimeout(() => resolve(123), 1000));
        }
      
        const data = await promiseHelper();
        logger("in myAsyncFunc, data:", data);
        return data;
      }
      
      function main() {
        myAsyncFunc()
          .then(data => {
            logger("in main, data:", data);
          })
          .catch(console.error);
      }
      
      logger('start!');
      main();
      
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 一 文件与组件命名 扩展名: 使用.js作为js文件的扩展名。如果同一个文件夹下有同名而不同作用的js文件,则通过...
    RN_GP阅读 8,757评论 0 1
  • php-fig 1、FIG:PSR[0-4]代码规范的得发明者、规范者,framework interoperab...
    汤华茂阅读 3,505评论 0 0
  • iOS编程规范0规范 0.1前言 为􏰀高产品代码质量,指导广大软件开发人员编写出简洁、可维护、可靠、可 测试、高效...
    iOS行者阅读 9,916评论 21 35
  • ESLint规范说明1、工具简介ESLint是一个QA工具,用来避免低级错误和统一代码的风格。ESLint被设计为...
    小漠穷秋阅读 8,084评论 0 0
  • 人不为己天诛地灭,吃亏是福,我选择后者,姥姥从小就是这教育的,吃亏是福
    FineYoga刘洋阅读 1,170评论 1 2