✅ 变量声明:letconst 的正确使用

  1. 优先使用 const 除非需要重新赋值
  2. 避免使用 var,ES6 已弃用全局作用域提升
  3. 块级作用域let/const 仅在 {} 内有效
    if (true) {
      const x = 10; // 仅在if块内有效
    }
    
    let_const

⚡ 箭头函数:简洁且避免 this 混乱

  1. 单表达式函数简写:
    const sum = (a, b) => a + b;
    
  2. 无需绑定 this:箭头函数继承外层 this
  3. 推荐用于回调函数:如数组 .map().sort()
    arrow_function

📦 模块化:importexport 的规范写法

  1. 显式导出
    export function greet() { ... }
    
  2. 默认导出
    export default class Calculator { ... }
    
  3. 按需导入
    import { add, subtract } from './math';
    
    module_import

🧠 类与继承:ES6 类语法的现代写法

  1. 使用 class 定义类,继承通过 extends
  2. 构造函数constructor() 必须使用
  3. 静态方法:用 static 关键字定义
    static createInstance() { return new this(); }
    
    class_inheritance

📌 扩展阅读

⚠️ 注意事项

  • 避免滥用 eval:存在安全风险
  • 谨慎使用 with:可能破坏作用域链
  • 保持代码简洁:遵循 JavaScript 开发规范