javascript:
- 变量与作用域
let/const 替代 var:块级作用域更安全
if (true) {
let a = 10;
const b = 20;
}
console.log(a); // 报错(a未定义)
- 闭包
函数保留外层作用域的变量
function createCounter() {
let count = 0;
return () => count++;
}
const counter = createCounter();
counter(); // 1
- 异步编程
用 Async/Await 代替回调地狱
async function fetchData() {
const data = await fetch('url');
console.log(data);
}
- 事件循环
代码执行顺序规则
setTimeout(() => console.log("宏任务"), 0);
Promise.resolve().then(() => console.log("微任务"));
// 输出顺序:微任务 → 宏任务
- 原型链
对象继承的底层逻辑
class Animal {}
class Dog extends Animal {}
console.log(new Dog() instanceof Animal); // true
- 箭头函数
无自己的 this,适合简短函数
const obj = {
name: "Alice",
show: () => console.log(this.name) // 输出 undefined
};
obj.show();
- 解构赋值
快速提取数据
const [a, b] = [1, 2]; // a=1, b=2
const {name} = {name: "Bob"}; // name="Bob"
- 数组方法
map/filter/reduce 三件套
[1,2,3].map(n => n*2); // [2,4,6]
[1,2,3].filter(n => n>1); // [2,3]
[1,2,3].reduce((sum, n) => sum + n, 0); // 6