ReactNative编写规范
<一> React 代码规范
文件与组件命名
- 扩展名: 使用.js作为js文件的扩展名。如果同一个文件夹下有同名而不同作用的js文件,则通过中缀(小写)进一步区分,例如:HomePageView.component.js, HomePageView.style.js, HomePageView.action.js等
- 文件名: 使用驼峰命名法且首字母大写,如HomePageView.js
- 组件命名: 与文件名(除中缀外)完全一致。如果组件单独放置在目录中,则目录名也一致
|
1
|
import Footer from './Component/OrderAccount/OrderAccountView'; |
|
1
|
import Footer from './Component/OrderAccount/OrderAccount'; |
|
1
|
import Footer from './OrderAccount'; |
组件声明
- 使用class与extends关键字。不使用React.createClass方法。需要导出的组件直接在class关键字前使用export default。
|
1
|
export default React.createClass({ }); |
|
1
|
export default class HomePageView extends Component { } |
对齐
- 按下面的案例对齐
|
1
2
|
<Foo superLongParam="bar" anotherSuperLongParam="baz" /> |
|
1
2
|
<Foo superLongParam="bar" anotherSuperLongParam="baz" /> |
// 如果一行能摆下props,那就摆在一行
|
1
|
<Foo bar="bar" /> |
// 子组件照常缩进
|
1
2
3
4
5
6
|
<Foo superLongParam="bar" anotherSuperLongParam="baz"> <Spazz /></Foo> |
引号
- 对于JSX的字符串属性使用双引号("),其他情况下使用单引号。
|
1
|
<Foo bar='bar' /> |
|
1
|
<Foo bar="bar" /> |
|
1
|
<Foo style={{ left: "20px" }} /> |
|
1
|
<Foo style={{ left: '20px' }} /> |
空格
- 在自闭合的标签中包含一个空格。
|
1
|
<Foo/> |
|
1
|
<Foo /> |
|
1
|
<Foo /> |
|
1
|
<Foo /> |
state/props
- 对于多个单词组成的pros,使用驼峰命名法。不使用下划线或连接线。
|
1
2
3
4
|
<Foo UserName="hello" phone_number={12345678}/> |
|
1
2
3
4
|
<Foo userName="hello" phoneNumber={12345678}/> |
- 读取state和props时,使用const与解构,必要时可使用let。不使用var。
|
1
2
|
var userName = this.props.userName;let checked = this.state.checked; |
|
1
2
|
const { userName, age, sex } = this.props;const { checked } = this.state; |
括号
- 当JSX标签超过一行时,使用括号包裹。
|
1
2
3
4
5
|
render() { return <MyComponent className="long body" foo="bar"> <MyChild /> </MyComponent>;} |
|
1
2
3
4
5
6
7
|
render() { return ( <MyComponent className="long body" foo="bar"> <MyChild /> </MyComponent> );} |
// good, when single line
|
1
2
3
4
|
render() { const body = <div>hello</div>; return <MyComponent>{body}</MyComponent>;} |
标签
- 对于没有子组件的JSX标签,始终自闭合。
|
1
|
<Foo className="stuff"></Foo> |
|
1
|
<Foo className="stuff" /> |
如果组件有多行属性,则另起一行进行自闭合。
|
1
2
3
|
<Foo bar="bar" baz="baz" /> |
|
1
2
3
4
|
<Foo bar="bar" baz="baz"/> |
方法
- 为方法命名时,不使用下划线开头(哪怕是想用作私有方法)。
|
1
2
3
4
5
6
|
React.createClass({ _onClickSubmit() { // do stuff } // other stuff}); |
|
1
2
3
4
5
6
|
class extends React.Component { onClickSubmit() { // do stuff } // other stuff}); |
方法声明的顺序
- 原则上按如下顺序排列React组件的各个方法(生命周期):
<二>ES6代码规范
变量与常量声明
- 变量
尽量使用let来代替var
对于全局变量声明,采用global.xxx = xxx,但应避免声明过多全局变量污染环境
- 常量
对于常量应使用const进行声明,命名采用驼峰写法
对于使用 immutable 数据应用const进行声明
|
1
2
3
4
|
let someNum = 123; const AnotherStr = '不变的字符串'; let arr = ['不', '变', '数', '组']; var ANOTHER_OBJ = { '不变对象': true }; |
|
1
2
3
4
|
const someNum = 123; const anotherStr = '不变的字符串'; const arr = ['不', '变', '数', '组']; const anotherObj = { '不变对象': true }; |
字符串
- 处理多行字符串,使用模板字符串
以反引号( ` )标示
可读性更强,代码更易编写
注意排版引起空格的问题,使用场景为声明HTML模板字符串
|
1
2
3
|
const tmpl = '<div class="content"> \n' +'<h1>这是换行了。</h1> \n' + '</div>'; |
|
1
|
const tmpl = ` <div class="content"> <h1>这是换行了。</h1> </div>`; |
- 处理字符串拼接变量时,使用模板字符串
|
1
2
3
|
function sayHi(name) { return 'How are you, ' + name + '?'; } |
|
1
2
3
|
function sayHi(name) { return `How are you, ${name}?`;} |
解构
- 解构语句中不使用圆括号
|
1
2
|
[(a)] = [11]; // a未定义let { a: (b) } = {}; // 解析出错 |
|
1
|
let [a, b] = [11, 22]; |
- 对象解构
对象解构 元素与顺序无关
对象指定默认值时仅对恒等于undefined ( !== null ) 的情况生效
- 若函数形参为对象时,使用对象解构赋值
|
1
2
3
4
5
|
function someFun(opt) { let opt1 = opt.opt1; let opt2 = opt.opt2; console.log(op1); } |
|
1
2
3
4
5
6
7
|
function someFun(opt) { let { opt1, opt2 } = opt; console.log(`$(opt1) 加上 $(opt2)`);} function someFun({ opt1, opt2 }) { console.log(opt1);} |
- 若函数有多个返回值时,使用对象解构,不使用数组解构,避免添加顺序的问题
|
1
2
3
4
5
6
|
function anotherFun() { const one = 1, two = 2, three = 3; return [one, two, three]; }const [one, three, two] = anotherFun(); // 顺序乱了 // one = 1, two = 3, three = 2 |
|
1
2
3
4
5
6
|
function anotherFun() { const one = 1, two = 2, three = 3; return { one, two, three };}const { one, three, two } = anotherFun(); // 不用管顺序// one = 1, two = 2, three = 3 |
- 已声明的变量不能用于解构赋值(语法错误)
// 语法错误
|
1
|
let a; { a } = { b: 123}; |
- 数组解构
数组元素与顺序相关
- 交换变量的值
|
1
2
3
4
5
6
7
8
|
let x = 1; let y = 2; // 不好let temp;temp = x;x = y;y = temp; // 好[x, y] = [y, x]; // 交换变量 |
- 将数组成员赋值给变量时,使用数组解构
|
1
2
3
4
5
6
|
const arr = [1, 2, 3, 4, 5]; // 不好const one = arr[0];const two = arr[1]; // 好const [one, two] = arr; |
数组
- 将类数组(array-like)对象与可遍历对象(如Set, Map)转为真正数组
采用Array.from进行转换
|
1
2
3
4
5
6
7
8
|
// 不好 function foo() { let args = Array.prototype.slice.call(arguments); } // 好 function foo() { let args = Array.from(arguments); } |
- 数组去重
结合Set结构与Array.from
使用indexOf,HashTable等形式,不够简洁清晰
|
1
2
3
4
|
// 好function deduplication(arr) { return Array.from(new Set(arr));} |
- 数组拷贝
采用数组扩展...形式
|
1
2
3
4
5
6
7
8
9
|
const items = [1, 2, 3]; // 不好const len = items.length;let copyTemp = [];for (let i = 0; i < len; i++) { copyTemp[i] = items[i];} // 好let copyTemp = [...items]; |
- 将一组数值转为数组
采用Array.of进行转换
|
1
2
3
4
5
6
|
// 不好let arr1 = new Array(2); // [undefined x 2]let arr2 = new Array(1, 2, 3); // [1, 2, 3] // 好let arr1 = Array.of(2); // [2]let arr2 = Array.of(1, 2, 3); // [1, 2, 3] |
函数
- 当要用函数表达式或匿名函数时,使用箭头函数(Arrow Functions)
箭头函数更加简洁,并且绑定了this
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
// 不好const foo = function(x) { console.log(foo.name); // 返回'' ,函数表达式默认省略name属性}; [1, 2, 3].map(function(x) { return x + 1;}); var testObj = { name: 'testObj', init() { var _this = this; // 保存定义时的this引用 document.addEventListener('click', function() { return _this.doSth(); }, false); }, doSth() { console.log(this.name); }}; // 好const foo = (x) => { console.log(foo.name); // 返回'foo'}; [1, 2, 3].map( (x) => { return x + 1;}); var testObj = { name: 'testObj', init() { // 箭头函数自动绑定定义时所在的对象 document.addEventListener('click', () => this.doSth(), false); }, doSth() { console.log(this.name); }}; |
- 箭头函数书写约定
函数体只有单行语句时,允许写在同一行并去除花括号
当函数只有一个参数时,允许去除参数外层的括号
|
1
2
3
4
5
6
|
// 好const foo = x => x + x; // 注意此处会隐性return x + x const foo = (x) => { return x + x; // 若函数体有花括号语句块时须进行显性的return}; [1, 2, 3].map( x => x * x); |
- 用箭头函数返回一个对象,应用括号包裹
|
1
2
3
4
|
// 不好let test = x => { x: x }; // 花括号会变成语句块,不表示对象 // 好let test = x => ({ x: x }); // 使用括号可正确return {x:x} |
l 立即调用函数 IIFE
|
1
2
3
4
5
6
7
8
|
// 不好function foo(opts) { opts = opts || {};// 此处有将0,''等假值转换掉为默认值的副作用} // 好function foo(opts = {}) { console.log('更加简洁,安全');} |
- 对象中的函数方法使用缩写形式
更加简洁
函数方法不要使用箭头函数,避免this指向的混乱
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
// 不好const shopObj = { des: '对象模块写法', foo: function() { console.log(this.des); }}; const shopObj = { des: '对象模块写法', foo: () => { console.log(this.des); // 此处会变成undefined.des,因为指向顶层模块的this }}; // 好const des = '对象模块写法'; // 使用对象属性值简写方式const shopObj = { des, foo() {console.log(this.des); } }; |
类
- 类名应使用帕斯卡写法(PascalCased)
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
// 好class SomeClass { }l 类名与花括号须保留一个空格间距// 不好class Foo{ constructor(){ // constructor } sayHi() { // 仅保留一个空格间距 }} // 好class Foo { constructor() { // constructor } sayHi() { // 仅保留一个空格间距 }} |
- 定义类时,方法的顺序如下:
- constructor
- public get/set 公用访问器,set只能传一个参数
- public methods 公用方法,公用相关命名使用小驼峰式写法(lowerCamelCase)
- private get/set 私有访问器,私有相关命名应加上下划线 _ 为前缀
- private methods 私有方法
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
// 好 class SomeClass { constructor() {// constructor } get aval() { // public getter } set aval(val) { // public setter } doSth() { // 公用方法 } get _aval() { // private getter } set _aval() { // private setter } _doSth() { // 私有方法 }} |
- 如果不是class类,不使用new
|
1
2
3
4
5
6
7
8
|
// 不好 function Foo() { }const foo = new Foo(); // 好class Foo { }const foo = new Foo(); |
- 使用真正意思上的类Class写法,不使用prototype进行模拟扩展
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
Class更加简洁,易维护// 不好function Dog(names = []) { this._names = [...names]; }Dog.prototype.bark = function() { const currName = this._names[0]; alert(`one one ${currName}`); } // 好class Dog { constructor(names = []) {this._names = [...names]; } bark() { const currName = this._names[0]; alert(`one one ${currName}`); } } |
- this的注意事项
子类使用super关键字时,this应在调用super之后才能使用
可在方法中return this来实现链式调用写法
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
class Foo { constructor(x, y) { this.x = x; this.y = y; }} // 不好class SubFoo extends Foo { constructor(x, y, z) {this.z = z; // 引用错误super(x, y); }} // 好class SubFoo extends Foo { constructor(x, y, z) { super(x, y); this.z = z; // this 放在 super 后调用 } setHeight(height) { this.height = height; return this; } } |
模块
- 使用import / export来做模块加载导出,不使用非标准模块写法
跟着标准走的人,运气总不会太差
|
1
2
3
4
5
6
|
// 不好const colors = require('./colors');module.exports = color.lightRed; // 好import { lightRed } from './colors';export default lightRed; |
- import / export 后面采用花括号{ }引入模块的写法时,须在花括号内左右各保留一个空格
|
1
2
3
4
5
|
// 不好import {lightRed} from './colors';import { lightRed} from './colors'; // 好import { lightRed } from './colors';
|
ReactNative编写规范的更多相关文章
- 20151009 C# 第一篇 程序编写规范
20151009 程序编写规范 1. 代码书写规则: 1).尽量使用接口,然后使用类实现接口. 2).关键语句写注释 3).避免写超过5个参数的方法,如果要传递多个参数,则使用结构 4).避免代码量过 ...
- .NET代码编写规范 整理
.NET代码编写规范 整理 .NET代码编写规范 - [ASP.NET] 2009-02-26 | Tag: 版权声明:转载时请以超链接形式标明文章原始出处和作者信息及本声明 http://lensp ...
- MarkDown编写规范指南
Markdown 编写规范指南 简介: Markdown的目标是实现「易读易写」,成为一种适用于网络的「书写语言」. 一份使用Markdown格式撰写的文件可以直接以纯文本发布,它的最大灵感来源其实是 ...
- 代码编写规范说明书(c#.net与asp.net)
代码编写规范说明书(c#.net与asp.net) 目 录1 目的2 范围3 注释规范3.1 概述3.2 自建代码文件注释3.3 模块(类)注释3.4 类属性注释3.5 方法注释3.6 代码间注释4 ...
- Markdown 编写规范
说明及目的 作为一个在博客园混迹了俩三年的人,一直在这里看别人的博客,现在准备开始写自己的博客,目的呢,就是一下几点吧: 项目过程中的历史经验教训积累记载,吃一堑长一智,不想在同一个坑掉进去好几次 学 ...
- Python代码编写规范
Python代码编写规范 编码: a) 如无特殊情况,文件一律使用UTF-8编码 b) 如无需特殊情况,文件头部必须加入#-*-coding:utf-8-*- 缩进 a) 统一 ...
- 【转】python编写规范——中标软件有限公司测试中心
[转]python编写规范 一.说明 二.内容 1. 代码布局 1.1 缩进 1.2 表达式和语句中的空格 1.3 行的最大长度 1.4 空行... 1.5 编码... 2. 语句... 2.1 标准 ...
- 内核知识第六讲,内核编写规范,以及获取GDT表
内核知识第六讲,内核编写规范,以及获取GDT表 一丶内核驱动编写规范 我们都知道,在ring3下,如果我们的程序出错了.那么就崩溃了.但是在ring0下,只要我们的程序崩溃了.那么直接就蓝屏了. 那么 ...
- 个人css编写规范
前言:最近在做微信小程序,因为公司小,就我一个人弄前端的东西,js和页面都是我来弄,结果那天后台的人看到我的js代码,说我的代码写得不规范,函数什么的都很乱,弄得我羞愧难当,幸亏没看我的css,其实我 ...
随机推荐
- python(leetcode)-66加一问题
给定一个由整数组成的非空数组所表示的非负整数,在该数的基础上加一. 最高位数字存放在数组的首位, 数组中每个元素只存储一个数字. 你可以假设除了整数 0 之外,这个整数不会以零开头. 示例 1: 输入 ...
- 如何批量添加图片到ppt的方法
如何批量添加图片到ppt 许多时候会做一些幻灯片,需要大量的图片,但是往往一张以张的加图片,会很浪费时间,如何快速添加图片,一次解决呢? 步骤:插入-相册-点击相册 点击文件,批量选择你要插入的图片, ...
- ubuntu16.04 离线安装docker ce
ubuntu离线安装 docker17.05.0-ce 离线安装 不想自己下载的小伙伴可以从以下地址下载几个软件包: 链接: https://pan.baidu.com/s/1lF7t7ciMhUnW ...
- mysql 开发进阶篇系列 16 MySQL Server(myisam key_buffer)
一.概述 mysql 提供了很多参数来进行服务器的设置,当服务第一次启动的时候,所有启动参数值都是系统默认的.这些参数在很多生产环境下并不能满足实际的应用需求.在这个系列中涉及到了liunx 服务器, ...
- VS Code 安装 C++ 调试环境
在 VS CODE 中安装 C/C++ 插件 安装编译.调试环境 这里安装的是 MinGW, 官网如下:http://mingw.org/ 下载后,双击进行安装安装路径可以根据需要来修改: 然后选择需 ...
- jQuery.prop , jQuery.attr ,jQuery.data
理一下这几个概念吧.根据jquery官网. jquery.prop 获取匹配的元素中第一个元素特定的属性值,或者是设置多个元素的属性值. 有4个重载. .prop(propertyName) 获取属性 ...
- Redis持久化及其配置
引言 终于可以有时间继续看书,整理自己的见解, 写下2019年第一篇自己的随笔.从去年9月份跳槽到新公司后,几乎天天的加班让整个人都盲目了,原本计划好的事情总是会被打乱.都说坚持一件事情很难,特别是写 ...
- Docker介绍及常用操作演示(一)--技术流ken
Docker简介 Docker 是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的Linux机器上,也可以实现虚拟化.容器是完全使用沙箱机制,相互 ...
- [JavaScript] 前端模块编程实现
前端模块化 前端早期写代码都是全局变量满天飞,这种情况会造成全局命名空间污染,变量冲突等问题 var a = 1; var b = 2; function c(){} function d(){} 后 ...
- mysql循环插入数据、生成随机数及CONCAT函数
实现目标:一年12个月,每个月插入一条数据,score为1-5的随机数 循环语句: WHILE -- DO -- END WHILE DELIMITER ; CREATE PROCEDURE test ...