跳到主要内容

ES6+新特性

变量声明

// let和const
let count = 1;
const MAX_COUNT = 100;

// 块级作用域
{
let blockScoped = 'only available in this block';
}

解构赋值

// 数组解构
const [a, b] = [1, 2];

// 对象解构
const { name, age } = person;

// 默认值
const { title = 'Default' } = options;

箭头函数

// 基本语法
const add = (a, b) => a + b;

// this绑定
const obj = {
data: [],
init() {
// this指向obj
['a', 'b'].forEach(item => {
this.data.push(item);
});
}
};

Promise和异步处理

// Promise基本使用
const promise = new Promise((resolve, reject) => {
// 异步操作
});

// async/await
async function getData() {
try {
const result = await fetchData();
return result;
} catch (error) {
console.error(error);
}
}

模块化

// 导出
export const name = 'module';
export default class {}

// 导入
import { name } from './module';
import DefaultExport from './module';

新的数据结构

// Set
const set = new Set([1, 2, 3]);

// Map
const map = new Map();
map.set('key', 'value');

// WeakMap/WeakSet
const weakMap = new WeakMap();