代码规范

  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张图片
    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();
      

你可能感兴趣的:(代码规范)