代码规范
代码规范主要作用是规范开发、提高开发效率、提高代码可读性以及便于后期维护。
引用
使用const、let 代替var
let const 是块级作用域,而不像
var是函数作用域.
// const 和 let 只存在于他们定义的块中。{let a = 1;const b = 1;}console.log(a); // Errorconsole.log(b); // Error
//内层变量覆盖外层变量var tmp = new Date();function f(){console.log(tmp);if(false){var tmp = "hello";}}f(); //undefined

注释与空白
1、使用 /** ... */ 来进行多行注释。
// bad// make() returns a new element// based on the passed in tag name//// @param {String} tag// @return {Element} elementfunction make(tag) {// ...return element;}// good/*** make() returns a new element* based on the passed-in tag name*/function make(tag) {// ...return element;}
2、使用 // 进行单行注释。 将单行注释放在需要注释的行的上方新行。 在注释之前放一个空行,除非它在块的第一行。
// badconst active = true; // is current tabfunction getType() {console.log('fetching type...');// set the default type to 'no type'const type = this.type || 'no type';return type;}// good// is current tabconst active = true;function getType() {console.log('fetching type...');// set the default type to 'no type'const type = this.type || 'no type';return type;}function getType() {// set the default type to 'no type'const type = this.type || 'no type';return type;}
3、 用一个空格开始所有的注释。
// bad/***make() returns a new element*based on the passed-in tag name*/function make(tag) {// ...return element;}// good/*** make() returns a new element* based on the passed-in tag name*/function make(tag) {// ...return element;}
4、在主体前放置一个空格。
// badfunction test(){console.log('test');}// goodfunction test() {console.log('test');}// baddog.set('attr',{age: '1 year',breed: 'Bernese Mountain Dog',});// gooddog.set('attr', {age: '1 year',breed: 'Bernese Mountain Dog',});
表达式、运算符
1、三目表达式不应该嵌套。
通常为单行表达式
// badconst foo = maybe1 > maybe2? "bar": value1 > value2 ? "baz" : null;// 分离为两个三目表达式const maybeNull = value1 > value2 ? 'baz' : null;// betterconst foo = maybe1 > maybe2? 'bar': maybeNull;// bestconst foo = maybe1 > maybe2 ? 'bar' : maybeNull;
2、在使用一些混合运算符时,使用括号括起来。
// badconst foo = a && b < 0 || c > 0 || d + 1 === 0;// bad// 可能陷入一种 (a || b) && c 的思考if (a || b && c) {return d;}// goodconst foo = (a && b < 0) || c > 0 || (d + 1 === 0);// goodif (a || (b && c)) {return d;}
3、使用 === 和 !== 而不是 == 和 !=

命名规范
1、避免单字母的名字。用你的命名来描述功能。
// badfunction q() {// ...}// goodfunction query() {// ...}
2、在命名对象、函数和实例时使用驼峰命名法。
// badconst OBJEcttsssss = {};const this_is_my_object = {};function c() {}// goodconst thisIsMyObject = {};function thisIsMyFunction() {}
3、属性命名时不要使用前置或者后置下划线。
// badthis.__firstName__ = 'Panda';this.firstName_ = 'Panda';this._firstName = 'Panda';// goodthis.firstName = 'Panda';
4、缩略词和缩写都必须是全部大写或者全部小写。
// badimport SmsContainer from './containers/SmsContainer';// badconst HttpRequests = [// ...];// goodimport SMSContainer from './containers/SMSContainer';// goodconst HTTPRequests = [// ...];// also goodconst httpRequests = [// ...];
模块
1、只从一个路径导入所有需要的东西。
// badimport foo from 'foo';// … 其他导入 … //import { named1, named2 } from 'foo';// goodimport foo, { named1, named2 } from 'foo';// goodimport foo, {named1,named2,} from 'foo';
2、将所有的 imports 语句放在所有非导入语句的之上。
// badimport foo from 'foo';foo.init();import bar from 'bar';// goodimport foo from 'foo';import bar from 'bar';foo.init();
3、多行导入应该像多行数组和对象一样缩进。
// badimport {longNameA, longNameB, longNameC, longNameD, longNameE} from 'path';// goodimport {longNameA,longNameB,longNameC,longNameD,longNameE,} from 'path';
4、使用easycom 组件引入模式。
不管components目录下安装了多少组件,
easycom打包后会自动剔除没有使用的组件,对组件库的使用尤为友好。
代码块
1、当有多行代码块的时候,使用大括号包裹。
// badif (test)return false;// goodif (test) return false;// goodif (test) {return false;}// badfunction foo() { return false; }// goodfunction bar() {return false;}
2、如果使用if、else的多行代码,else语句放在if闭括号同一行。
// badif (test) {thing1();thing2();}else {thing3();}// goodif (test) {thing1();thing2();} else {thing3();}
3、if条件与return。
如果一个if块总是执行一个return 语句,接下来的else块便可以省略。
// badfunction foo() {if (x) {return x;} else {return y;}}// goodfunction foo() {if (x) {return x;}return y;}
release模式下删除调试信息
链接https://cn.vuejs.org/v2/style-guide/#%E7%A7%81%E6%9C%89-property-%E5%90%8D%E5%BF%85%E8%A6%81

