- 作用域:块级作用域(替代
var
的函数作用域)。
const
:声明常量,不可重新赋值(对象属性可修改)。
let name = 'Alice';
const PI = 3.14;
- 简洁语法:省略
function
和 return
。
this
绑定:继承外层上下文(解决传统函数 this
指向问题)。
const add = (a, b) => a + b;
arr.map(item => item * 2);
- **反引号(
)**:支持多行文本和变量嵌入(
${}`)。
const msg = `Hello, ${name}!
Your score is ${score}.`;
const [a, b] = [1, 2]; // a=1, b=2
const { name, age } = user; // 提取 user 的属性
- 默认参数:参数默认值设置。
- Rest 参数:将剩余参数转为数组。
function greet(name = 'Guest', ...args) {
console.log(`Hello, ${name}!`, args);
}
const arr = [...oldArr, newItem];
const objCopy = { ...original };
class Person {
constructor(name) { this.name = name; }
sayHi() { console.log(`Hi, I'm ${this.name}`); }
}
// math.js
export const add = (a, b) => a + b;
// app.js
import { add } from './math.js';
fetch(url)
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error(error));
Set
:唯一值集合。
Map
:键值对集合(键可为任意类型)。
const set = new Set([1, 2, 2, 3]); // {1, 2, 3}
const map = new Map();
map.set('key', 'value');
for...of
:遍历可迭代对象(数组、字符串、Map等)。
- 生成器函数:
function*
和 yield
控制执行流程。
function* gen() {
yield 1;
yield 2;
}
const iterator = gen();
iterator.next(); // { value: 1, done: false }
const id = Symbol('id');
const obj = { [id]: 123 };
- 对象字面量增强:简写属性和方法。
const name = 'Alice';
const obj = { name, sayHi() { /* ... */ } };
- 二进制/八进制字面量:
0b1010
(二进制)、0o12
(八进制)。
Array.prototype.includes
:判断数组是否包含某元素。