ES6+ 新特性
箭头函数 (ES2015)
// 传统函数
function add(a, b) {
return a + b;
}
// 箭头函数
const add = (a, b) => a + b;
// 单参数可以省略括号
const square = x => x * x;
// 无参数
const greet = () => console.log("Hello!");
// 注意: 箭头函数没有自己的 this
const obj = {
value: 42,
getValue: function() {
return this.value; // ✅ 正确
},
getValueArrow: () => this.value // ❌ 错误,this 指向外层
};
模板字符串 (ES2015)
// 基本使用
const name = "张三";
const age = 25;
const message = `你好,我是${name},今年${age}岁`;
// 多行字符串
const html = `
<div>
<h1>标题</h1>
<p>内容</p>
</div>
`;
// 带表达式
const price = 99.99;
const tax = price * 0.1;
const total = `价格: ¥${price.toFixed(2)} (含税: ¥${tax.toFixed(2)})`;
解构赋值 (ES2015)
// 数组解构
const [a, b, ...rest] = [1, 2, 3, 4, 5];
// a=1, b=2, rest=[3,4,5]
// 对象解构
const user = { name: "张三", age: 25, city: "北京" };
const { name, age } = user;
// name="张三", age=25
// 重命名
const { name: userName, age: userAge } = user;
// 默认值
const { city = "上海" } = user;
// 嵌套解构
const data = { user: { profile: { name: "李四" } } };
const { user: { profile: { name } } } = data;
扩展运算符 (ES2015)
// 数组展开
const arr1 = [1, 2, 3];
const arr2 = [4, 5, 6];
const combined = [...arr1, ...arr2]; // [1,2,3,4,5,6]
// 对象展开
const obj1 = { a: 1, b: 2 };
const obj2 = { c: 3, d: 4 };
const merged = { ...obj1, ...obj2 }; // {a:1,b:2,c:3,d:4}
// 函数参数
const sum = (...numbers) => numbers.reduce((a, b) => a + b, 0);
console.log(sum(1, 2, 3, 4, 5)); // 15
类 (ES2015)
// 基本类
class Person {
constructor(name, age) {
this.name = name;
this.age = age;
}
greet() {
return `你好,我是${this.name}`;
}
static getSpecies() {
return "人类";
}
}
// 继承
class Student extends Person {
constructor(name, age, grade) {
super(name, age);
this.grade = grade;
}
study() {
return `${this.name}正在学习`;
}
}
// 私有字段 (ES2022)
class BankAccount {
#balance = 0;
deposit(amount) {
this.#balance += amount;
}
getBalance() {
return this.#balance;
}
}
现代数组方法
// Array.prototype.find (ES2015)
const users = [
{ id: 1, name: "张三" },
{ id: 2, name: "李四" }
];
const user = users.find(u => u.id === 2); // {id:2,name:"李四"}
// Array.prototype.includes (ES2016)
const arr = [1, 2, 3];
console.log(arr.includes(2)); // true
// Array.prototype.flat (ES2019)
const nested = [1, [2, [3]]];
console.log(nested.flat(2)); // [1,2,3]
// Array.prototype.findIndex (ES2016)
const index = users.findIndex(u => u.name === "张三"); // 0
// Array.from (ES2015)
const arrayLike = { length: 3, 0: 'a', 1: 'b', 2: 'c' };
const arr2 = Array.from(arrayLike); // ['a','b','c']
对象新特性
// 对象简写
const name = "张三";
const age = 25;
const person = { name, age };
// 相当于: { name: name, age: age }
// 方法简写
const obj = {
greet() {
console.log("Hello!");
}
};
// Object.assign (ES2015)
const target = { a: 1 };
const source = { b: 2 };
Object.assign(target, source); // {a:1,b:2}
// Object.keys/values/entries (ES2017)
const data = { a: 1, b: 2, c: 3 };
console.log(Object.keys(data)); // ['a','b','c']
console.log(Object.values(data)); // [1,2,3]
console.log(Object.entries(data)); // [['a',1],['b',2],['c',3]]
字符串新方法
// startsWith/endsWith/includes (ES2015)
const str = "Hello, World!";
console.log(str.startsWith("Hello")); // true
console.log(str.endsWith("World!")); // true
console.log(str.includes("lo")); // true
// repeat (ES2015)
console.log("a".repeat(5)); // "aaaaa"
// padStart/padEnd (ES2017)
console.log("5".padStart(3, "0")); // "005"
console.log("5".padEnd(3, "0")); // "500"
// trimStart/trimEnd (ES2019)
const text = " hello ";
console.log(text.trimStart()); // "hello "
console.log(text.trimEnd()); // " hello"
ES2020+ 新特性
// 可选链 (ES2020)
const user = { profile: { name: "张三" } };
const name = user?.profile?.name; // "张三"
const age = user?.profile?.age; // undefined
// 空值合并 (ES2020)
const value = null ?? "默认值"; // "默认值"
const value2 = 0 ?? "默认值"; // 0 (0 不是 null/undefined)
// BigInt (ES2020)
const bigNumber = 9007199254740991n;
const bigAdd = bigNumber + 1n;
// String.prototype.replaceAll (ES2021)
const text = "a-b-c";
console.log(text.replaceAll("-", "/")); // "a/b/c"
// Object.hasOwn (ES2022)
const obj = { a: 1 };
console.log(Object.hasOwn(obj, "a")); // true
console.log(Object.hasOwn(obj, "toString")); // false
// 数组 at() 方法 (ES2022)
const arr = [1, 2, 3];
console.log(arr.at(-1)); // 3 (最后一个元素)