You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// 不好constvariables;constglobalObj=null;// 不是常量letglobalObj=null;for(vari=0;i<5;i++){console.log(i);}console.log(i);// 4// 好letvariables;varglobalObj=null;for(leti=0;i<5;i++){console.log(i);}console.log(i);// ReferenceError: i is not defined
// 不好functionanotherFun(){constone=1,two=2,three=3;return[one,two,three];}const[one,three,two]=anotherFun();// 顺序乱了// one = 1, two = 3, three = 2// 好functionanotherFun(){constone=1,two=2,three=3;return{ one, two, three };}const{ one, three, two }=anotherFun();// 不用管顺序// one = 1, two = 2, three = 3
// 不好functionDog(names=[]){this._names=[...names];}Dog.prototype.bark=function(){constcurrName=this._names[0];alert(`one one ${currName}`);}// 好classDog{constructor(names=[]){this._names=[...names];}bark(){constcurrName=this._names[0];alert(`one one ${currName}`);}}
classFoo{constructor(x,y){this.x=x;this.y=y;}}// 不好classSubFooextendsFoo{constructor(x,y,z){this.z=z;// 引用错误super(x,y);}}// 好classSubFooextendsFoo{constructor(x,y,z){super(x,y);this.z=z;// this 放在 super 后调用}setHeight(height){this.height=height;returnthis;}}
ECMAScript6 编码规范
ES6 Coding Style English Version
规范内容
声明
字符串
解构
数组
Set
,Map
)转为真正数组函数
arguments
, 采用rest语法...
代替类
PascalCased
)constructor
get/set
公用访问器,set
只能传一个参数lowerCamelCase
)get/set
私有访问器,私有相关命名应加上下划线_
为前缀new
prototype
进行模拟扩展this
的注意事项模块
import / export
来做模块加载导出,不使用非标准模块写法import / export
后面采用花括号{ }
引入模块的写法时,须在花括号内左右各保留一个空格import
不使用统配符*
进行整体导入import
与export
混合在一行Copyright
Copyright (c) 2016 Hancoson
The text was updated successfully, but these errors were encountered: