1.基础数据类型
新增了Symbol,BigInt。前者表示独一无二的值,用来定义对象的唯一属性名。后者可以表示任意大小的整数。
2.块级作用域
新增let,const关键字。前者声明的变量具有全局作用域,没有变量提升,有暂时性死区特性。后者声明常量,不能重新赋值。
3.解构赋值
更直观?
4.箭头函数
箭头函数不绑定this。this与上级通同。解决了匿名函数内存泄漏问题。
5.剩余参数
剩余参数配合解构赋值使用,能打印出参数来。
剩余参数可以将不定数量的参数表示为一个数组,方便声明不知道参数的函数。
6.函数优化
函数可以设置默认值。箭头函数。
7.map和reduce
前者接受一个函数,然后用这个函数去处理数组中的每一个元素后返回。
后者为数组中的每一个元素依次执行回调函数,不包括数组中被删除和未被赋值的元素。可选初始值,四个参数。
8.模块化
export和import。export用于规定模块的对外接口。import用于导入其它模块提供的功能。
9.对象,数组,字符串扩展
对象。获取所有key,value,二维,将多个src对象的值拷贝到dest中。
数组。find,findindex,include。
字符串。includes,startsWith,endswith
10.Promise
容器。保存着某个未来才会结束的事件。异步操作。可以从这个对象获取异步操作的消息。
then等异步操作结束做一些事情。catch失败后事件。
resolve,reject。异步成功后的回调函数和失败后的回调函数。
promise的精髓是状态,用维护状态传递状态的方式使回调函数能及时调用。
all提供了并行执行异步操作的能力,在所有异步操作执行完后才执行回调。all会把所有的异步结果放入一个数组中传给then。
race,跑的快的先
promise三种状态,等待,完成,失败。只能等待-完成或等待-失败。
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 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
| const PENDING = 'pending'; const FULFILLED = 'fulfilled'; const REJECTED = 'rejected';
class MyPromise { constructor(executor){ executor(this.resolve, this.reject) }
status = PENDING;
value = null; reason = null;
onFulfilledCallback = null;
onRejectedCallback = null;
resolve = (value) => { if (this.status === PENDING) { this.status = FULFILLED; this.value = value; } }
reject = (reason) => { if (this.status === PENDING) { this.status = REJECTED; this.reason = reason; } } } then(onFulfilled, onRejected) { if (this.status === FULFILLED) { onFulfilled(this.value); } else if (this.status === REJECTED) { onRejected(this.reason); } else if (this.status === PENDING) { this.onFulfilledCallback = onFulfilled; this.onRejectedCallback = onRejected; } }
resolve = (value) => { if (this.status === PENDING) { this.status = FULFILLED; this.value = value; this.onFulfilledCallback && this.onFulfilledCallback(value); } }
reject = (reason) => { if (this.status === PENDING) { this.status = REJECTED; this.reason = reason; this.onRejectedCallback && this.onRejectedCallback(reason) } }
module.exports = MyPromise
|
all
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
| Promise.myAll = (promises) => { return new Promise((rs, rj) => { let count = 0 let result = [] const len = promises.length if (len === 0) { return rs([]) } promises.forEach((p, i) => { Promise.resolve(p).then((res) => { count += 1 result[ i ] = res if (count === len) { rs(result) } }).catch(rj) }) }) }
|