【前端】Ionic3 升级到Ionic4的代码规范

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/m18633778874/article/details/83377439

前言

     小编最近升级Ionic版本3--4时,发现有一些代码规范方面的事项,特意摘出来,总结一些,希望遇到类似的问题时,可以及时解决,不占用主要的开发时间。

格式规范

    一、双引号""全部改为单引号''

    二、"=="改为"==="

    三、if语句的规范

       错误写法    

 if (this.isChoujiang || this.isRedpacket || this.isActivity) {
        this.isNoMessage = false;
   } 
  else {
        this.isNoMessage = true;
   }

     正确写法,else必须和if语句的第二个花括号同一行

 if (this.isChoujiang || this.isRedpacket || this.isActivity) {
        this.isNoMessage = false;
  } else {
        this.isNoMessage = true;
  }

  四、方法()后必须以分号;结尾 

  五、注释语句// 必须以空格隔开

  // 退出界面时,轮播图停止
   ionViewWillLeave() {
    this.slides.stopAutoplay();
  }

声明规范

一、只能使用const、let声明变量,不能使用var

const url = 'kernel-web/user/findDiffInfo/' + this.userId;
let buttons = [];
buttons=['抽奖','发红包'];

二、同一方法内,不能声明同名变量 

否则会报错,提示:Shadowed name: 'i'

                                         

// i j两个变量声明
 if (arr[i].reason !== '' && arr[i].reason.length > 14) {
        const str = arr[i].reason;
        let newLength = 0;
        let newStr = '';
        const chineseRegex = /[^\x00-\xff]/g;
        let singleChar = '';
        const strLength = str.replace(chineseRegex, '**').length;
        for (let j = 0; j < strLength; j++) {
          singleChar = str.charAt(j).toString();
          if (singleChar.match(chineseRegex) != null) {
            newLength += 2;
          } else {
            newLength++;
          }
          if (newLength > this.reasonlen) {
            break;
          }
          newStr += singleChar;
        }

        if (strLength > this.reasonlen) {
          newStr += '...';
        }
        arr[i].reason = newStr;

}

小结

    通过迁移代码,升级ionic框架,自己在这个过程中,也是学到了很多,成长了一些,更加明确代码规范的重要性及其表现的几个方面。

                                                                           感谢您的访问!

猜你喜欢

转载自blog.csdn.net/m18633778874/article/details/83377439