正文
ES6(ES2015)及后续版本为JavaScript带来了许多现代化特性,大大提升了开发效率和代码质量。 主要新特性: 1. 箭头函数:简洁的函数语法 2. 解构赋值:从数组或对象中提取值 3. 模板字符串:支持插值的字符串 4. Promise和async/await:异步编程 5. 类和模块:面向对象编程 6. 扩展运算符:数组和对象的展开 示例: ```javascript // 箭头函数 const add = (a, b) => a + b; // 解构赋值 const [first, second] = [1, 2]; const { name, age } = { name: 'Alice', age: 25 }; // 模板字符串 const greeting = `Hello, ${name}! You are ${age} years old.`; // Promise和async/await async function fetchData() { try { const response = await fetch('/api/data'); const data = await response.json(); return data; } catch (error) { console.error('Error:', error); } } // 类 class Person { constructor(name, age) { this.name = name; this.age = age; } sayHello() { return `Hello, I'm ${this.name}`; } } // 扩展运算符 const arr1 = [1, 2, 3]; const arr2 = [...arr1, 4, 5]; // [1, 2, 3, 4, 5] ``` 这些特性让JavaScript更加现代化和易用。