做淘寶貨源網(wǎng)站seo關(guān)鍵詞優(yōu)化排名軟件
ES2015:
1.塊級作用域const、let
const聲明對象可修改屬性,但不能重新賦值對象。
2.解構(gòu)賦值
const arr = [a1, a2, a3];
const [a1, ...rest] = arr; // rest = [a2, a3];
3.模板字符串``
const date = "星期一";
console.log(`今天是${date};`);
4.函數(shù)參數(shù)默認(rèn)值
func(a = 1, ...rest) {}
5.箭頭函數(shù)
const func = () => {};
6.對象代理proxy
const personProxy = new Proxy(person, {get() {},set() {}
});
// 劫持對象修改其屬性
7.Object.assign、Object.is
// 對象合并或賦值
const obj = Object.assign(o1, o2);// 對象判斷
NaN === NaN; // false;
Object.is(NaN, NaN); // true;
8.異步解決方案Promise
new Promise((resolve, reject) => {resolve(data);
)).then((data) => {})
.then...
9.類class、extents、super
10.數(shù)據(jù)結(jié)構(gòu)Set、Map、Symbol
// 不重復(fù)集合Set;
// 有序鍵值對,鍵不只是字符串,可以是任意值集合Map;
// 唯一值Symbol;
Symbol('symbol') === Symbol('symbol'); // false;
Symbol.for('symbol') === Symbol('symbol'); // true; 全局表
11.for…of遍歷
for (const item of set) {}
for (const [item, value] of map) {}
12.模塊化module
export { num, numFunc };
import { num as n, numFunc } from './common.js';
13.字符串方法startsWith、endsWith、includes
ES2016
1.數(shù)組includes方法
2.指數(shù)運算符**
2**3 // Math.pow(2, 3);
ES2017
1.異步async await
async func () => {await getSomeApi();
};
2.對象方法Object.values()、Object.entries
類似于Object.keys()返回對象所有的鍵、值、鍵值對數(shù)組。
3.字符串方法padStart()、padEnd()
// 補全字符串開頭和結(jié)尾
padStart(3, '1'); // 001
ES2018
1.異步迭代
for await(const item of items) {}
2.promise.finally
.catch()
.finally(() => {});
3.對象rest
const obj = {a: 1, b: 2, c: 3};
const {a, ...rest} = obj; // rest = {b: 2, c: 3};
const obj2 = {...rest, d: 4};
4.正則命名捕獲組和斷言
// ?<name>
const reg = /(?<year>/d{4})-(?<month>\d{2})/;
const result = reg.exec('2023-06'); // result.groups = {year: '2023', month: '06'};
// ?= 正向斷言
// ?<= 反向斷言
'Paul666'.match(/Paul?6+/); // Paul666
'Paul666'.match(/Paul(?=6+)/); // Paul
'Paul666'.match(/(?<=Paul)6+/); // 666
ES2019
1.數(shù)組扁平化
const arr = [[1], [2], [[3], [4]];
arr.flat(2); // [1, 2, 3, 4];
arr.flatMap(x => x); // [1, 2, [3], [4]];
2.Object.fromEntries()、Object.entries()
// 對象數(shù)組轉(zhuǎn)化
const arr = [['age', '18'], ['name', 'jack']];
const obj = Object.fromEntries(arr); // {age: '18', name: 'jack'};
const arr = Object.entries(obj); // [['age', '18'], ['name', 'jack']];
3.字符串trimStart()、trimEnd()
去除前后的空格
4.Symbol.description
// 方便獲取symbol描述
const symbol = Symbol('a');
symbol.description; // a;
5.catch參數(shù)可選
// 當(dāng)我們catch不需要使用綁定參數(shù)時
try {} catch {};
ES2020
1.字符串matchAll
匹配所有符合條件的子串,返回匹配情況的數(shù)組。
2.import動態(tài)導(dǎo)入
// 需要該模塊時才會加載,返回一個promise對象。
import('./common.js').then(module => {});
3.Bigint
// 針對js大數(shù)精度丟失問題
7897489489448464464n;
Bigint('7897489489448464464');
4.promise.allSettled
promise.all // 所有的promise對象均成功才執(zhí)行then;
promise.allSettled // 所有的promise對象均有出現(xiàn)結(jié)果(成功或失敗);
promise.any // 只要有一個promise對象成功,則返回該promise對象;全部失敗則返回AggregateError對象;
promise.race // 跑的快的promise對象的結(jié)果(成功或失敗);
5.統(tǒng)一全局對象globalThis
// 不同環(huán)境下的全局this對象
window === globalThis; // 瀏覽器
global === globalThis; // node.js
6.可選鏈?
防止類型報錯,不存在則返回undefined。
7.空值合并??
0 || 1; // 1 左側(cè)為假返回右值;
0 ?? 1; // 0 左側(cè)為null或undefined,才返回右值;
ES2021
1.字符串replaceAll
全部替換
2.promise.any
3.數(shù)字分隔符
const num = 1_000_000_000; // 更清晰;
ES2022
1.正則修飾符/d
/i // 忽略大小寫;
/g // 全局;
/m // 多行;
/d // 返回indices表示匹配的下標(biāo)開始和結(jié)束位置索引;
2.Object.hasOwn()
判斷對象是否有子鼠星,比hasOwnProperty()(obj=null時會報錯)安全;
3.cause自定義錯誤
throw new Error('failed', {cause: msg});
ES2023
1.HashBang語法
// 指定腳本文件的解釋器
// #!/usr/bin/env node
console.log('js');