下载工作台
前端开发入门

ES6+ 与现代 JavaScript

试读上半部分 · 解锁后可读全文

第 11 章 · ES6+ 与现代 JavaScript

本章目标:掌握 ES6 及之后的核心语法——解构、展开、模板字符串、箭头函数、模块概念、Map/Set,并用现代写法重构 academy-demo 中的课程数据与 Todo 状态逻辑。

前置:第 8~10 章;浏览器需支持 ES2015+(近五年主流浏览器均可)。


11.1 为什么学 ES6+

维度ES5 写法ES6+ 写法收益
变量var 函数作用域const / let 块级作用域减少变量提升 bug
字符串'Hello ' + name模板字符串可读性、多行
函数function(){}箭头函数简洁回调
对象手动合并属性展开运算符不可变更新
模块全局变量 / IIFEimport / export命名空间清晰
行业案例 · 贤紫优选商城:管理后台 2019 年仍为 ES5 + jQuery 打包单体 800KB;迁移到 ES6 模块 + Tree Shaking 后,首屏 JS 降至 220KB,构建时间从 90s 降至 12s。

11.2 模板字符串

const course = { id: 'fe-11', title: 'ES6+ 与现代 JavaScript', hours: 5 };
const label = `课程「${course.title}」共 ${course.hours} 课时,slug: fe-${course.id.split('-')[1]}`;

const multiline = `
  <article class="course-card">
    <h3>${course.title}</h3>
  </article>
`;
特性语法注意
插值${expression}表达式内可调用函数
多行反引号内直接换行注意缩进空白会保留
标签模板` tag... `高级场景(样式库)

安全提醒:模板字符串插入用户输入前必须转义(见第 9 章 escapeHtml)。


11.3 解构赋值

11.3.1 数组解构

const rgb = [200, 24, 91];
const [r, g, b] = rgb;
const [first, , third] = ['HTML', 'CSS', 'JS'];

// 交换变量
let a = 1, b = 2;
[a, b] = [b, a];

// 剩余参数
const [head, ...rest] = [1, 2, 3, 4]; // head=1, rest=[2,3,4]

11.3.2 对象解构

const course = {
  id: 'fe-11',
  title: 'ES6+',
  meta: { level: 'intermediate', free: false },
};

const { title, id: courseId } = course; // 重命名 id → courseId
const { meta: { level } } = course;     // 嵌套解构

function renderCourse({ title, hours = 4 }) {
  return `${title}(${hours}h)`;
}

// 函数参数默认值 + 解构
renderCourse({ title: 'DOM', hours: 3 });
场景写法academy-demo 用途
取 API 字段const { data } = await res.json()课程列表
配置对象const { theme = 'light' } = options主题初始化
交换/忽略const [, second] = arr排行榜第二名

11.4 展开运算符(Spread)与剩余(Rest)

const baseTags = ['HTML', 'CSS'];
const allTags = [...baseTags, 'JavaScript', 'Vite'];

const defaults = { theme: 'light', locale: 'zh-CN' };
const userPrefs = { theme: 'dark' };
const settings = { ...defaults, ...userPrefs }; // theme 被覆盖为 dark

// 不可变更新 Todo(React 思维的前置)
function toggleTodoImmutable(todos, id) {
  return todos.map(t =>
    t.id === id ? { ...t, done: !t.done } : t
  );
}

// Rest 参数
function sum(...nums) {
  return nums.reduce((a, b) => a + b, 0);
}
运算符位置含义
...arr数组/对象字面量中展开元素
...obj对象字面量中浅拷贝属性
...args函数参数末尾收集剩余参数

注意:展开是浅拷贝,嵌套对象仍共享引用。


11.5 箭头函数

// 传统
const double = function (n) { return n * 2; };

// 箭头
const double = n => n * 2;
const courses = list.filter(c => c.free);
const sorted = [...list].sort((a, b) => a.hours - b.hours);

document.querySelector('#btn').addEventListener('click', () => {
  console.log('clicked');
});
对比项普通函数箭头函数
this动态绑定词法继承外层
arguments无,用 Rest
new可作构造函数不可
适用对象方法、构造函数回调、数组方法
// 对象方法仍推荐简写,不用箭头(避免 this 问题)
const store = {
  items: [],
  add(item) { this.items.push(item); }, // ✓
  // add: (item) => this.items.push(item) // ✗ this 不是 store
};

11.6 其他常用 ES6+ 语法

以下内容需解锁后阅读

试读已结束。解锁本章 ¥5.00,或开通年度会员畅读全部教程。
年度会员 ¥199.00/年; 小紫 AI 工作台有效会员 ¥99.00/年

正文仅在服务端鉴权后下发,未付费无法获取下半部分内容。