考察点:对象/数组解构的实战用法与常见坑。这是 ES6 在日常联调后端接口、函数参数处理中最常用的特性,顺接箭头函数与函数式写法。
要点:
const { name: userName = '匿名' } = user 解决「后端字段名与前端命名不一致」「字段缺失兜底」两类问题。const { profile: { age } } = user 直接取深层字段;配合默认值(profile: { age } = {})可避免读到 undefined 的子属性而报错。const [first, ...rest] = list 取首项、其余归集;const [a, , c] = arr 用空位跳过元素;const [a, b] = [b, a] 一行实现变量交换。function sum({ a, b = 0 } = {}) {} 让调用方传对象更直观,默认参数防止解构 undefined 报错。常见坑:
undefined / null 会直接抛 TypeError(不能对「非对象」解构),参数解构务必给整体默认值 = {}。null 不会触发默认;重命名后默认值是跟在「新名字」后面({ name: userName = 'x' })。题目与答案就在本页;收藏、批注和错题重练在应用内