JavaScript 的 8 大“阴间陷阱”,你绝对踩过!99% 程序员崩溃瞬间

788 阅读3分钟

💣 陷阱1:变量提升——你以为的“顺序执行”都是假的!

console.log(name); // 输出什么?你以为会报错?太天真了!  
var name = "程序员鱼皮";  
// 实际输出:undefined(变量提升的魔法)  

真相:JS 引擎会偷偷把 var 声明提到顶部,但赋值留在原地!
血泪教训

  • 新人写循环时疯狂踩坑:
    for (var i = 0; i < 3; i++) {  
      setTimeout(() => console.log(i), 100); // 输出3个3!  
    } 
    

救命稻草:立刻改用 let,和 var 说再见!


💣 陷阱2:闭包——内存泄漏的“隐形杀手”

function createTimer() {    
  const bigData= new Array(10000000); // 假装是个大数据
  let count = 0;  
  return () => count++;  
}  
const timer = createTimer(); 
// 即使不再使用,大数据 也永远不会被回收!  

求生指南

  1. 用 Chrome DevTools 的 Memory 面板抓“内存怪兽”
  2. 闭包用完手动置空:timer = null;

💣 陷阱3:事件循环(Event Loop)——面试必考的“魔法执行顺序”

console.log("Start");  
setTimeout(() => console.log("Timeout"), 0);  
Promise.resolve().then(() => console.log("Promise"));  
console.log("End");  
// 输出顺序:Start → End → Promise → Timeout  

灵魂图解

主线程任务 → 微任务(Promise)→ 宏任务(setTimeout

窒息操作

Promise.resolve().then(() => {  
  Promise.resolve().then(() => console.log("内层微任务"));  
});  
// 微任务队列会层层嵌套,直到清空!  

💣 陷阱4:this 指向——让人疯癫的“动态叛徒”

const GirlFriend = {  
  name: "小美",  
  angry: function() {  
    console.log(this.name + "生气了!");  
    setTimeout(function() {  
      console.log(this.name + "决定分手!"); // 输出 undefined!  
    }, 1000);  
  }  
};  
GirlFriend.angry();  

为什么setTimeout 的回调函数 this 指向全局(如 window)!
爱情拯救方案

  1. 用箭头函数:setTimeout(() => { ... })(绑定外层 this)
  2. 提前保存 this:const self = this;

💣 陷阱5:隐式转换——JS 的“黑暗炼金术”

console.log(1 + "1");    // "11"(字符串拼接)  
console.log("5" - 3);    // 2(字符串转数字)  
console.log([] == 0);    // true([] → "" → 0)  
console.log({} + []);    // "0"(对象转原始值)  

离谱到极致的案例

if ("0") { ... }       // true(非空字符串)  
if (new Boolean(false)) { ... } // true(对象永远为真)  

保命原则:永远用 ===,遇到玄学先 console.log(typeof xxx)


💣 陷阱6:异步错误处理——沉默的“致命 Bug”

// 忘记 catch 的 Promise 会静默死亡!  
fetch("https://noExistUrl")  
  .then(res => res.json())  
  .then(data => console.log(data));  
// 页面没有任何错误提示,但功能失效!  

// 正确姿势:  
fetch("...")  
  .then(...)  
  .catch(err => console.error("抓住错误:", err)); // 必须 catch!  

升级版坑爹案例

async function GetData() {  
  const res = await fetch("..."); // 假设这里报错  
  return res.json();  
}  
// 即使外层不写 try-catch,错误也会被吞掉!  

💣 陷阱7:原型链污染——你的对象被“下毒”了!

function User() {}  
User.prototype.Permissions = "普通用户";  

const Hack = new User();  
Hack.__proto__.Permissions = "超级管理员"; // 污染原型链  

const custom = new User();  
console.log(custom.Permissions); // 超级管理员(全体中招!)  

真实事件:Lodash 库曾爆出原型链污染漏洞,导致大规模攻击!
防御代码

  • 使用 Object.freeze(Object.prototype) 冻结原型
  • 避免直接操作 __proto__,改用 Object.create()

💣 陷阱8:箭头函数——你以为的“语法糖”其实是“炸药包”

const obj = {  
  val: 42,  
  getVal: () => {  
    console.log(this.val); // 输出 undefined!  
  }  
};  
obj.getVal();  

为什么:箭头函数没有自己的 this,直接继承外层!
正确写法

getVal: function() {  
  setTimeout(() => {  
    console.log(this.val); // 正确绑定对象 this  
  }, 100);  
}  

结语

欢迎评论区留言说说你常用的中间件组合,有问题也可以随时留言一起探讨!


觉得有用别忘了点赞收藏哦,关注我了解更多JavaScript开发实用技巧!