变量声明方式 let/const

与 var 不同,新的变量声明方式带来了一些不一样的特性,其中最重要的两个特性就是提供了块级作用域与不再具备变量提升。通过 2 个简单的例子来说明这两点。

1
2
3
4
5
{
let a = 20;
}

console.log(a); // a is not defined

而这个简单的例子,会被编译为:

1
2
3
4
5
{
let _a = 20;
}

console.log(a); // a is not defined
1
2
3
4
5
6
7
// ES5
console.log(a); // undefined
var a = 20;

// ES6
console.log(a); // a is not defined
let a = 20;
变量提升demo示例
变量提升demo示例

我们常常使用let来声明一个值会被改变的变量,而使用const来声明一个值不会被改变的变量,也可以称之为常量。

当值为基础数据类型时,那么这里的值,就是指值本身。
而当值对应的为引用数据类型时,那么这里说的值,则表示指向该对象的引用。这里需要注意,正因为该值为一个引用,只需要保证引用不变就可以,仍然可以改变该引用所指向的对象。

当我们试图改变 const 声明的变量时,则会报错。

写几个例子,大家可以仔细揣摩一下:

1
2
let a = null;
a = 20;
1
2
3
4
5
6
7
8
const obDev = {
a: 20,
b: 30
}

obDev.a = 30;

console.log(obDev); // Object {a: 30, b: 30}
1
2
3
const fn = function() {}
const a = obDev.a;
... ...

箭头函数的使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// es5
var fn = function(a, b) {
return a + b;
}

// es6 箭头函数写法,当函数直接被return时,可以省略函数体的括号
const fn = (a, b) => a + b;

// es5
var foo = function() {
var a = 20
var b = 30;
return a + b;
}

// es6
const foo = () => {
const a = 20;
const b = 30;
return a + b;
}

箭头函数可以替换函数表达式,但是不能替换函数声明。

其次还有一个至关重要的一点,就是箭头函数中,没有this。如果你在箭头函数中使用了this,那么该this一定就是外层的this。因此我们也就无从谈起用call/apply/bind来改变this指向。记住这个特性,能让你在react组件之间传值时少走无数弯路。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
var person = {
name: 'tom',
getName: function() {
return this.name;
}
}

// 我们试图用ES6的写法来重构上面的对象
const person = {
name: 'tom',
getName: () => this.name
}

// 但是编译结果却是
var person = {
name: 'tom',
getName: function getName() {
return undefined.name;
}
};

在ES6中,会默认采用严格模式,因此this也不会自动指向window对象了,而箭头函数本身并没有this,因此this就只能是undefined,这种情况,如果你还想用this,就不要用使用箭头函数的写法。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// 可以稍做改动
const person = {
name: 'tom',
getName: function() {
return setTimeout(() => this.name, 1000);
}
}

// 编译之后变成
var person = {
name: 'tom',
getName: function getName() {
var _this = this; // 使用了我们在es5时常用的方式保存this引用

return setTimeout(function () {
return _this.name;
}, 1000);
}
};

模板字符串

模板字符串是为了解决使用+号拼接字符串的不便利而出现的。

1
2
3
4
5
6
7
8
9
// es6
const a = 20;
const b = 30;
const string = `${a}+${b}=${a+b}`;

// es5
var a = 20;
var b = 30;
var string = a + "+" + b + "=" + (a + b);

解析结构

1
2
3
4
5
6
7
// 首先有这么一个对象
const props = {
className: 'tiger-button',
loading: false,
clicked: true,
disabled: 'disabled'
}

当我们想要取得其中的2个值:loadingclicked时:

1
2
3
4
5
6
7
8
9
// es5
var loading = props.loading;
var clicked = props.clicked;

// es6
const { loading, clicked } = props;

// 给一个默认值,当props对象中找不到loading时,loading就等于该默认值
const { loading = false, clicked } = props;

是不是简单了许多?正是由于解析结构大大减少了代码量,因此它大受欢迎,在很多代码中它的影子随处可见。

1
2
3
4
5
6
7
8
9
10
11
12
// 比如
// section1
import React, { Component } from 'react';

// section2
export { default } from './Button';

// section3
const { click, loading } = this.props;
const { isCheck } = this.state;

// more 任何获取对象属性值的场景都可以使用解析结构来减少我们的代码量

另外,数组也有属于自己的解析结构。

1
2
3
4
5
6
7
8
9
// es6
const arr = [1, 2, 3];
const [a, b, c] = arr;

// es5
var arr = [1, 2, 3];
var a = arr[0];
var b = arr[1];
var c = arr[2];

数组以序列号一一对应,这是一个有序的对应关系。
而对象根据属性名一一对应,这是一个无序的对应关系。

根据这个特性,使用解析结构从对象中获取属性值更加具有可用性。

函数默认参数

之前我们不能直接为函数指定默认参数,很多时候为了保证传入的参数具备一个默认值,常常使用如下的方法:

1
2
3
4
5
6
7
function add(x, y) {
var x = x || 20;
var y = y || 30;
return x + y;
}

console.log(add()); // 50

这种方式并不是没有缺点,比如传入一个x值为false,这个时候任然会取到默认值,就不是我们的本意了。

ES6的默认值写法:

1
2
3
4
5
function add(x = 20, y = 30) {
return x + y;
}

console.log(add());

在实际开发中给参数添加适当的默认值,可以让我们对函数的参数类型有一个直观的认知。

1
2
3
4
5
6
7
8
9
const ButtonGroupProps = {
size: 'normal',
className: 'xxxx-button-group',
borderColor: '#333'
}

export default function ButtonGroup(props = ButtonGroupProps) {
... ...
}

扩展运算符

扩展运算符(spread)是三个点(…)。它好比 rest 参数的逆运算,将一个数组转为用逗号分隔的参数序列。

1
2
3
4
const arr1 = [1, 2, 3];
const arr2 = [...arr1, 10, 20, 30];

// 这样,arr2 就变成了[1, 2, 3, 10, 20, 30];

当然,展开对象数据也是可以得到类似的结果:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
const obj1 = {
a: 1,
b: 2,
c: 3
}

const obj2 = {
...obj1,
d: 4,
e: 5,
f: 6
}

// 结果类似于 const obj2 = Object.assign({}, obj1, {d: 4})

展开运算符还常常运用在解析结构之中,例如我们在Raect封装组件的时候常常不确定props到底还有多少数据会传进来,就会利用展开运算符来处理剩余的数据。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// 这种方式在react中十分常用
const props = {
size: 1,
src: 'xxxx',
mode: 'si'
}


const { size, ...others } = props;

console.log(others)

// 然后再利用暂开运算符传递给下一个元素,再以后封装react组件时会大量使用到这种方式,正在学习react的同学一定要搞懂这种使用方式
<button {...others} size={size} />

展开运算符还用在函数的参数中,来表示函数的不定参。只有放在最后才能作为函数的不定参,否则会报错。

1
2
3
4
5
6
// 所有参数之和
const add = (a, b, ...more) => {
return more.reduce((m, n) => m + n) + a + b
}

console.log(add(1, 23, 1, 2, 3, 4, 5)) // 39

对象字面量 与 class

ES6针对对象字面量做了许多简化语法的处理。

1. 当属性与值的变量同名时

1
2
3
4
5
6
7
8
9
10
11
12
13
14
const name = 'Jane';
const age = 20

// es6
const person = {
name,
age
}

// es5
var person = {
name: name,
age: age
};

那么这种方式在任何地方都可以使用,比如在一个模块对外提供接口时

1
2
3
4
5
6
7
8
const getName = () => person.name;
const getAge = () => person.age;

// commonJS的方式
module.exports = { getName, getAge }

// ES6 modules的方式
export default { getName, getAge }

2. 除了属性之外,对象字面量写法中的方法也可以有简写方式。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// es6
const person = {
name,
age,
getName() { // 只要不使用箭头函数,this就还是我们熟悉的this
return this.name
}
}

// es5
var person = {
name: name,
age: age,
getName: function getName() {
return this.name;
}
};

在 Ant-Design 的源码实现中,就大量使用了这种方式来拼接当前元素的 className,例如:

1
2
3
4
5
6
7
let alertCls = classNames(prefixCls, {
[`${prefixCls}-${type}`]: true,
[`${prefixCls}-close`]: !this.state.closing,
[`${prefixCls}-with-description`]: !!description,
[`${prefixCls}-no-icon`]: !showIcon,
[`${prefixCls}-banner`]: !!banner,
}, className);

Ant-Design是一个认可度非常高的UI组件库,官方使用react的方式进行了实现,除此之外,还有vue也有对应的实现,有兴趣的同学可以去他们的官网了解学习。https://ant.design/index-cn

3. Class

ES6 为我们创建对象提供了新的语法糖,这就是 Class 语法。如果你对ES5中面向对象的方式比较熟悉的话,Class 掌握起来也是非常迅速的,因为除了写法的不同,它并不会增加新的难以理解的知识点。我们先利用一个简单的例子来看看写法的不同。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// ES5
// 构造函数
function Person(name, age) {
this.name = name;
this.age = age;
}

// 原型方法
Person.prototype.getName = function() {
return this.name
}

// ES6
class Person {
constructor(name, age) { // 构造函数
this.name = name;
this.age = age;
}

getName() { // 原型方法
return this.name
}
}

除此之外,还需要特别注意在实际使用中的几种写法方式的不同,在下面的例子注释中,我说明了他们分别对应的 ES5中 的含义。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Person {
constructor(name, age) { // 构造函数
this.name = name;
this.age = age;
}

getName() { // 这种写法表示将方法添加到原型中
return this.name
}

static a = 20; // 等同于 Person.a = 20

c = 20; // 表示在构造函数中添加属性 在构造函数中等同于 this.c = 20

// 箭头函数的写法表示在构造函数中添加方法,在构造函数中等同于this.getAge = function() {}
getAge = () => this.age

}

箭头函数需要注意的仍然是this的指向问题,因为箭头函数this指向不能被改变的特性,因此在react组件中常常利用这个特性来在不同的组件进行传值会更加方便。

4. 继承 extends

相比ES5,ES6的继承就要简单很多,我们直接来看一个例子。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Person {
constructor(name, age) {
this.name = name;
this.age = age;
}

getName() {
return this.name
}
}

// Student类继承Person类
class Student extends Person {
constructor(name, age, gender, classes) {
super(name, age);
this.gender = gender;
this.classes = classes;
}

getGender() {
return this.gender;
}
}

我们只需要一个extends关键字,就可以实现继承了,不用像ES5那样去担心构造函数继承和原型继承,除此之外,我们还需要关注一个叫做super的方法。

在继承的构造函数中,我们必须如上面的例子那么调用一次super方法,它表示构造函数的继承,与ES5中利用call/apply继承构造函数是一样的功能。

1
2
3
4
5
6
// 构造函数中
// es6
super(name, age);

// es5
Person.call(this);

super还可以直接调用父级的原型方法,super.getName,但是我自己从来没这样用过,也就不扩展说了。
继承在react中有大量的使用场景,许多组件都利用继承来创建。

1
2
3
4
5
6
7
8
9
10
11
12
13
import React, { Component } from 'react';

class App extends Component {

defaultProps = {}
state = {}
componentWillMount() {}
componentDidMount() {}

btnClick = e => {}

render() {}
}

Promise

前端基础进阶(十五):详解 Promise对象

模块 Modules

前端基础进阶(十五):详解 ES6 Modules