前端规范

2018-07-25  本文已影响0人  静候那一米阳光

代码缩进

分号

不推荐

let baz = { a: 1 }
function foo() { return true };
if (foo) { bar = 0 };

推荐

let baz = { a: 1 };
function foo() { return true; }
if (foo) { bar = 0; }

命名

变量名/属性名

函数名/方法名

样式名

组件名/类名

文件名

空行

代码块之间空行

拖尾换行

在非空文件中,存在拖尾换行是一个常见的 UNIX 风格,它的好处是可以方便在串联和追加文件时不会打断 Shell 的提示。在日常的项目中,保留拖尾换行的好处是,可以减少版本控制时的代码冲突。

空格

操作符空格

不推荐

let sum=1+2

推荐

let sum = 1 + 2

关键字空格

不推荐

if(condition) { ... }

推荐

if (condition) { ... }

行末空格

不推荐

let a = 1;   

推荐

let a = 1;

函数命名/调用空格

不推荐

function func () {
  // do something
}
func ()

推荐

function func() {
  // do something
}
func()

逗号,冒号,分号空格

不推荐

[1,2,3]
let baz = { a:1 }
for (let a = 1;a <= 10;a++) { }

推荐

[1, 2, 3]
let baz = { a: 1 }
for (let a = 1; a <= 10; a++) { }

单行代码块空格

不推荐

function foo() {return true;}
if (foo) {bar = 0}

推荐

function foo() { return true; }
if (foo) { bar = 0 }

圆括号空格

不推荐

getName( name );

推荐

getName(name);

注释首尾空格

不推荐

//comment
/*comment*/

推荐

// comment
/* comment */

空格个数

不推荐

const id =    1234;

推荐

const id = 1234;

无子元素组件闭合

不推荐

<View></View>

推荐

<View />

字符串

顺序

函数内代码区域排序

模块js文件代码区域排序

React生命周期方法与其他方法顺序问题

constructor() { }
componentWillMount() { }
render() { }
componentDidMount() { }
componentWillReceiveProps() { }
shouldComponentUpdate() { }
componentWillUpdate() { }
componentDidUpdate() { }
componentWillUnmount() { }
// 其他方法...

目录结构

非公共模块

不同模块的组件文件、样式文件、图片使用相同的目录存储

公共模块

每个模块使用自己独立的组件目录、样式目录、图片目录(便于移植)

换行问题

温馨提示

{!!length && <View />}
上一篇 下一篇

猜你喜欢

热点阅读