es6的新特性和如何使用
let 和 const 声明变量
let a = 1; const b = 2;
箭头函数
const sum = (a, b) => a + b;
模板字符串
const name = 'World'; console.log(`Hello ${name}`);
解构赋值
const { name, age } = person;
默认参数值
function greet(name = 'World') { console.log(`Hello ${name}`); }
扩展运算符
const arr1 = [1, 2, 3]; const arr2 = [4, 5, 6]; const arr3 = [...arr1, ...arr2];
类和继承
class Animal {
constructor(name) {
this.name = name;
}
speak() {
console.log(`${this.name} makes a noise`);
}
}
class Dog extends Animal {
speak() {
console.log(`${this.name} barks`);
}
}
const dog = new Dog('Rufus');
dog.speak();
模块化
// module.js export const sum = (a, b) => a + b;
// main.js import { sum } from './module.js'; console.log(sum(1, 2));