概述
全称 ECMAScript 6.0 2015.06 发版;国际化组织ECMA 制定了浏览器脚本语言的标准,即ECMAScript;
以下只整理部分ES6扩展;
函数的扩展
- 参数默认值
1
| function fn(x = 1, y = 2, z) {};
|
- 箭头函数
- catch 参数省略
- 参数的尾逗号
数组的扩展
- 扩展运算符
1 2 3 4 5
| console.log(...[1, 2, 3]);
[...'script']
|
- Array.from()
- 将类数组和部署了
Iterator接口的对象转成真正的数组
1 2 3 4 5 6 7 8
| let obj = { 0: 1, 1: 2, 2: 3, length: 3 }; Array.from(obj)
let set = new Set([12, 13]); Array.from(set);
Array.from('hello');
|
- Array.of()
1 2 3 4
| Array.of() Array.of(1) Array.of(13, 18) Array.of(1).length
|
- .find()和 .findIndex()
1 2
| [3, 4, 6, 8].find(n => n < 5); [3, 4, 6, 8].findIndex(n => n < 5);
|
- .fill
- .entries(),.keys() 和 .values()
1 2 3 4 5 6
| for (let index of [1, 2].keys()) { console.log(index) }
[1, 2].values().next() [1, 2].values().next()
|
- .includes()
1 2
| [1, 2, 3].includes(1); [1, 2, 3].includes(0);
|
- .flat(),.flatMap()
1 2 3 4 5
| [1, 2, [5, 5]].flat(1);
[2, 3, 4].flatMap((x) => [x, x * 2])
|
对象的新增
- super关键字
1 2 3 4 5 6 7 8 9 10 11
| const hello = { value: 'hello' } const obj = { value: 'world', say() { return super.value; } } Object.setPrototypeOf(obj, hello); obj.say();
|
- 解构赋值
1 2 3 4 5 6 7
| const { x, y, ...z } = { x: 1, y: 2, z: 3, w: 4 }; x y z const { z: t, w } = z; t w
|
- Object.is()
1 2 3
| Object.is('t', 't'); Object.is({}, {}); Object.is(NaN, NaN)
|
- Object.assign()
1 2 3
| let t = { x: 1, y: 2 }; let s = { z: 3 }; Object.assign(t, s, {});
|
- __proto__属性,Object.setPrototypeOf(),Object.getPrototypeOf()
1 2 3 4 5
| let t = { y: 1 } let f = { x: 2 } const o = Object.setPrototypeOf(f, t); o.y o.x
|
1 2 3 4 5 6 7 8 9 10 11 12
| function Rectangle() { }
const rec = new Rectangle();
Object.getPrototypeOf(rec) === Rectangle.prototype
Object.setPrototypeOf(rec, Object.prototype); Object.getPrototypeOf(rec) === Rectangle.prototype
|
- Object.keys(),Object.values(),Object.entries()
1 2 3 4 5 6
| let obj = { a: 1, b: 2, c: 3 }; Object.keys(obj) Object.values(obj) Object.entries(obj)
const _map=new Map(Object.entries(obj));
|
- Object.fromEntries()
- Object.entries() 的逆操作
1 2
| Object.fromEntries([[name,'tom'],[age,18]]);
|