ES6(ECMAScript 2016)是 JavaScript 的一次重大升级,带来了许多现代化特性。以下是核心知识点梳理:

1. 变量声明:let 与 const 📌

  • let:块级作用域变量,避免变量提升带来的问题
    let x = 10;
    if (true) {
      let x = 20; // 不影响外部 x
    }
    
  • const:常量声明,不可重新赋值
    const PI = 3.1415;
    PI = 3.14; // 报错!
    

2. 箭头函数:更简洁的写法 📜

  • 简化函数表达式:
    const square = x => x * x;
    
  • this 绑定(词法作用域):
    const obj = {
      value: 10,
      calc: () => this.value * 2 // this 指向 window 或 undefined
    };
    

3. 类与继承:面向对象的优雅实现 🏗️

  • 声明类:
    class Person {
      constructor(name) {
        this.name = name;
      }
      sayHello() {
        console.log(`Hello, ${this.name}`);
      }
    }
    
  • 子类继承:
    class Student extends Person {
      constructor(name, grade) {
        super(name); // 调用父类构造函数
        this.grade = grade;
      }
    }
    

4. 模块化开发:import 与 export 📦

  • 导出模块:
    // math.js
    export function add(a, b) {
      return a + b;
    }
    
  • 引入模块:
    import { add } from './math.js';
    console.log(add(2, 3)); // 输出 5
    

5. 解构赋值:简化数据提取 🧩

  • 数组解构:
    const [a, b, c] = [1, 2, 3];
    
  • 对象解构:
    const { name, age } = { name: "Alice", age: 25 };
    
es6_features

如需深入了解 ES6 的具体特性或实践案例,可参考本站的 ES6 特性详解教程