ES6 中有哪些常用特性?请列举并说明
常用 ES6+ 特性
1. let / const — 块级作用域变量声明
2. 箭头函数 — (a, b) => a + b
3. 解构赋值
const { name, age = 18 } = person // 对象解构 + 默认值
const [first, ...rest] = arr // 数组解构 + 剩余元素
function foo({ x, y } = {}) {} // 函数参数解构4. 模板字符串
const html = `<div>${name}</div>`5. 展开运算符
const merged = { ...obj1, ...obj2 } // 对象合并
const combined = [...arr1, ...arr2] // 数组合并
foo(...args) // 函数参数展开6. 默认参数
function foo(a = 1, b = 2) {}7. Promise + async/await
8. Class
class Person {
#private = 'secret' // 私有字段
static create() {}
}9. 模块系统
import { foo } from './module.js'
export const bar = 123
export default function() {}10. Map / Set / WeakMap / WeakSet
const map = new Map([['key', 'value']])
const set = new Set([1, 2, 3])11. 迭代器与生成器
function* gen() { yield 1; yield 2 }
for (const v of gen()) console.log(v)12. 可选链与空值合并(ES2020)
const name = user?.profile?.name ?? 'anonymous'13. 其他
Symbol— 唯一标识Proxy/Reflect— 元编程BigInt— 大整数Array.flat()/Array.flatMap()Object.fromEntries()