大家好,我是刘布斯。
我在面试过程中,经常喜欢问 “ES6+ 有哪些新特性?”,原因其实很简单:
很多候选人只会说 let/const
、箭头函数、解构赋值这些 ES6 的内容。
但真要让面试官眼前一亮,就得拿出 ES2016 - ES2025 的关键特性,结合实际场景讲清楚。
Array.prototype.includes
indexOf
更直观。[1, 2, 3].includes(2); // true
indexOf > -1
可读性好。**
2 ** 3
等价于 Math.pow(2, 3)
。async function getUser() {
const res = await fetch('/api/user');
return res.json();
}
Object.entries({a:1, b:2}).forEach(([k,v]) => console.log(k,v));
for await...of
for await (let chunk of stream) {
console.log(chunk);
}
const {id, ...info} = user; // 拆分对象
[1,[2,[3]]].flat(2); // [1,2,3]
Object.fromEntries([['a',1],['b',2]]); // {a:1,b:2}
?.
user?.profile?.name
??
null
/ undefined
时才取默认值。user.age ?? 18
0
和 null
。'foo_foo'.replaceAll('foo','bar'); // bar_bar
const data = await fetch('/api/data').then(r=>r.json());
#
class User { #pwd = '123'; }
Object.groupBy(users, u => u.age>18?'adult':'minor');
setA.union(setB)
const {promise, resolve, reject} = Promise.withResolvers();
[1,2,3].values().map(x=>x*2).filter(x=>x>2).toArray();
// [4,6]
Promise.try(()=>JSON.parse('err')).catch(console.error);
RegExp.escape('a+b'); // 'a\\+b'
import data from './config.json' with { type: 'json' };
从 ES2016 到 2025,JavaScript 的进化路线大致是:
另外,还没有使用过我们刷题网站(https://fe.ecool.fun/)或者刷题小程序的同学,如果近期准备或者正在找工作,千万不要错过,题库主打无广告和更新快哦~。
有会员购买、辅导咨询的小伙伴,可以通过下面的二维码,联系我们的小助手。