🔥前端干货|程序员必看,5 段可直接复用代码,面试开发两不误
现在前端越来越卷,只会 CV 已经很难打。整理日常高频、面试常考核心知识点,代码复制即用,新手老手都适用。 1、防抖节流(页面性能必备)
▼js复制代码// 防抖 const debounce = (fn, delay=300) => { let timer; return (...args)=>{ clearTimeout(timer); timer = setTimeout(()=>fn(...args),delay) } } // 节流 const throttle = (fn, gap=500) => { let last = 0; return (...args)=>{ const now = Date.now(); if(now‑last >= gap){ fn(...args); last=now } } }
2、手写简易深拷贝
▼js复制代码function deepClone(obj, map=new WeakMap()){ if(!obj||typeof obj!=='object') return obj if(map.has(obj)) return map.get(obj) const res = Array.isArray(obj)?[]:{} map.set(obj,res) for(const k in obj) res[k]=deepClone(obj[k],map) return res }
3、异步请求封装 Promise
▼js复制代码async function fetchData(url){ try{ const res = await fetch(url) return await res.json() }catch(e){ console.error('请求失败',e) } }
<<<顺便说句,技术大厂,前后端-测试机会,一线双一线城市坑位充足,感兴趣可以看看这个机会~
4、Vite 本地代理解决跨域
▼js复制代码// vite.config.js export default { server:{ proxy:{ '/api':{ target:'http://127.0.0.1:3000', changeOrigin:true, rewrite:p=>p.replace(/^\/api/,'') } } } }
5、JS 事件循环速记 同步代码优先执行 → 微任务 (Promise) → 宏任务 (setTimeout)
▼js复制代码console.log(1) setTimeout(()=>console.log(2),0) Promise.resolve().then(()=>console.log(3)) console.log(4) //输出:1 4 3 2
📌简单学习思路 基础:HTML 语义化 + Flex/Grid 布局 + ES6+ 进阶:Vue3/React + TS,吃透异步、原型、闭包 工程化:Vite、性能优化、跨域、缓存策略 实践:多写项目,不要只会背八股,懂场景懂原理 写在最后 前端技术更新很快,但底层知识万变不离其宗。多动手敲代码,远比收藏一堆文章更有用。 点赞收藏不迷路,持续输出前端实战干货
评论
问答助学
相关内容
0个评论
全部评论
点击登录,快来和大家讨论吧~
表情
图片
暂无评论
