当前位置: 首页 > news >正文

手撕小汇总

防抖

// 函数防抖的实现 function debounce(fn, wait) { let timer = null; return function() { let context = this, args = arguments; // 如果此时存在定时器的话,则取消之前的定时器重新记时 if (timer) { clearTimeout(timer); timer = null; } // 设置定时器,使事件间隔指定事件后执行 timer = setTimeout(() => { fn.apply(context, args); }, wait); }; }

节流

时间戳版(首段执行,无尾调用)

// 函数节流的实现; function throttle(fn, delay) { let curTime = Date.now(); return function() { let context = this, args = arguments, nowTime = Date.now(); // 如果两次时间间隔超过了指定时间,则执行函数。 if (nowTime - curTime >= delay) { curTime = Date.now(); fn.apply(context, args); } }; }

定时器版(首段延迟,有尾调用)

function throttle(fn, delay) { let timer = null; return function () { const context = this; const args = arguments; if (!timer) { timer = setTimeout(() => { fn.apply(context, args); timer = null; }, delay); } }; }

融合版(首尾调用都有)

function throttle(fn, delay) { // 上一次执行的时间戳 let lastTime = Date.now(); // 兜底定时器标识 let timer = null; return function () { const context = this; const args = arguments; // 当前触发时间 const now = Date.now(); // 距离下一次允许执行还剩多久 const remain = delay - (now - lastTime); // 场景1:剩余时间 <=0,达到间隔,可以立即执行 if (remain <= 0) { // 如果存在等待中的兜底定时器,直接清除(防止重复执行) if (timer) { clearTimeout(timer); timer = null; } lastTime = now; fn.apply(context, args); } // 场景2:时间还不足,且当前没有等待的定时器,则创建兜底任务 else if (!timer) { timer = setTimeout(() => { lastTime = Date.now(); timer = null; fn.apply(context, args); }, remain); } } }

类型判断

function getType(value) { // 单独处理两大特殊基础类型 if (value === null) return 'null' if (value === undefined) return 'undefined' // 统一调用toString const rawType = Object.prototype.toString.call(value) // 截取 [object Xxx] 中间字符串 return rawType.slice(8, -1).toLowerCase() } console.log(getType(null)); // 'null' console.log(getType(undefined)); // 'undefined' console.log(getType(123)); // 'number' console.log(getType(123n)); // 'bigint' console.log(getType('aaa')); // 'string' console.log(getType(true)); // 'boolean' console.log(getType(Symbol())); // 'symbol' console.log(getType([])); // 'array' console.log(getType({})); // 'object' console.log(getType(function(){})); // 'function' console.log(getType(new Date())); // 'date' console.log(getType(/\w+/)); // 'regexp' console.log(getType(new Map())); // 'map' console.log(getType(new Set())); // 'set'

call

记得挂载函数,记得解构argument,记得解构args

Function.prototype.myCall = function(context) { // 1. 校验调用者必须是函数 if (typeof this !== 'function') { throw new TypeError('this is not a function'); } // 2. 仅null/undefined替换为全局,其余值原样保留 context = context ?? globalThis; // 3.原始值自动装箱 context = Object(context); // 4. 创建唯一临时key,防止属性覆盖冲突 const tempKey = Symbol('tempFunc'); // 5. 挂载函数(此处this是调用call的函数) context[tempKey] = this; // 6. 截取参数,执行 const args = [...arguments].slice(1); const result = context[tempKey](...args); // 7. 删除临时方法 delete context[tempKey]; return result; };

apply

记得解构arguments,没传参记得加()

function apply(ctx) { if (typeof this !== 'function') { throw new TypeError('type error') } let result = null ctx=ctx??globalThis ctx= Object(ctx) const tempKey=Symbol('tempFn') ctx[tempKey]=this if(arguments[1]){ result = ctx[tempKey](...arguments[1]) }else{ result=ctx[tempKey]() } delete ctx[tempKey] return result }

bind

解构argument,参数使用concat合并解构的argument

// bind 函数实现 Function.prototype.myBind = function(context) { // 判断调用对象是否为函数 if (typeof this !== "function") { throw new TypeError("Error"); } // 获取参数 let args = [...arguments].slice(1), fn = this; return function Fn() { // 根据调用方式,传入不同绑定值 return fn.apply( this instanceof Fn ? this : context, args.concat(...arguments) ); }; };

函数柯里化

function curry(fn) { // fn.length 获取函数预期形参数量 const len = fn.length return function curried(...args) { // 收集参数 >= 需要参数 → 执行 if(args.length >= len) { return fn(...args) } // 参数不够,继续返回新函数收集 return function(...newArgs) { return curried(...args, ...newArgs) } } }

浅拷贝

// 浅拷贝的实现; function shallowCopy(object) { // 只拷贝对象 if (!object || typeof object !== "object") return; // 根据 object 的类型判断是新建一个数组还是对象 let newObject = Array.isArray(object) ? [] : {}; // 遍历 object,并且判断是 object 的属性才拷贝 for (let key in object) { if (object.hasOwnProperty(key)) { newObject[key] = object[key]; } } return newObject; }

深拷贝

// 深拷贝的实现 function deepCopy(object) { if (!object || typeof object !== "object") return; let newObject = Array.isArray(object) ? [] : {}; for (let key in object) { if (object.hasOwnProperty(key)) { newObject[key] = typeof object[key] === "object" ? deepCopy(object[key]) : object[key]; } } return newObject; }

数组求和

可多维求和

const sum = arr.flat(Infinity).reduce((s, v) => s + v, 0)

数组扁平化

let arr = [1, [2, [3, 4]]]; function flatten(arr) { while (arr.some(item => Array.isArray(item))) { arr = [].concat(...arr); } return arr; } console.log(flatten(arr)); // [1, 2, 3, 4,5] //或者直接用flat

用Promise实现图片的异步加载

let imageAsync=(url)=>{ return new Promise((resolve,reject)=>{ let img = new Image(); img.src = url; img.οnlοad=()=>{ console.log(`图片请求成功,此处进行通用操作`); resolve(image); } img.οnerrοr=(err)=>{ console.log(`失败,此处进行失败的通用操作`); reject(err); } }) } imageAsync("url").then(()=>{ console.log("加载成功"); }).catch((error)=>{ console.log("加载失败"); })

实现发布-订阅模式

class EventBus { constructor() { // 存储 { 事件名: [回调函数列表] } this.events = Object.create(null); } // 订阅:on(事件名, 回调) on(eventName, callback) { if (!this.events[eventName]) { this.events[eventName] = []; } this.events[eventName].push(callback); } // 发布/触发:emit(事件名, 参数...) emit(eventName, ...args) { const cbs = this.events[eventName]; if (!cbs) return; // 循环执行所有订阅回调,传入参数 cbs.forEach(fn => fn(...args)); } // 取消订阅:off(事件名, 指定回调) off(eventName, callback) { const cbs = this.events[eventName]; if (!cbs) return; this.events[eventName] = cbs.filter(item => item !== callback); } // 一次性订阅:once,触发后自动解绑 once(eventName, callback) { const wrapper = (...args) => { callback(...args); this.off(eventName, wrapper); }; this.on(eventName, wrapper); } }
http://www.cnnetsun.cn/news/3463982.html

相关文章:

  • Windows 11 22H2版本选择与安装指南
  • 爱芯派Pro开发板评测:72TOPS NPU与边缘计算实战
  • DSP信号混频实验:创龙TL6748-TEB实验箱实战指南
  • 终极指南:3步实现PSD到FairyGUI的智能转换,提升游戏UI开发效率300%
  • AMD KV260视觉套件开箱与AI模型部署实战
  • HarmonyOS掌上记账APP开发实践第28篇:自定义组件封装策略 — 从通用 Header 到业务组件的分层设计
  • 设备巡检报修系统小程序开发
  • JavaScript项目测试实战:从单元测试到集成测试的完整指南
  • 【关注可白嫖源码】--课程设计--毕业设计--springboot二次元周边售卖网站[编号:project18448](案件分析)
  • 家里的总电闸要拉一拉就知道——配置中心不是把参数挪个地方,是让你改一个值全屋都不用动
  • MC3172 RISC-V开发板环境搭建与多线程编程实战
  • TVA:具身智能技术生态的强力引擎(9)
  • Java基础中级进阶篇六之网络编程(反射、网络编程)
  • 【Gemini会议助手深度解析】:20年AI产品经理亲测的5大隐藏功能,90%用户从未用过!
  • AI创业中的知识产权策略:模型版权、数据授权与开源合规的避坑指南
  • 具身智能的定义、特征与原理解析(17)
  • 游戏数据看板需要哪些指标:核心逻辑、执行步骤与关键指标
  • 如何解决AI到word的格式问题?用 AI 导出鸭终结排版崩坏噩梦
  • DDE-GoCode与其他桌面环境依赖包的终极对比分析:为什么选择正确的依赖包至关重要
  • FPGA开发板按键与LED控制实战:从消抖到状态机
  • AI Agent技能架构与工程实践全解析
  • Unveiling Intrinsic Text Bias in Multimodal Large Language Models through Attention Key-Space Ana...
  • opendesign-deployment项目架构详解:构建稳定可靠的设计系统部署方案
  • Mac版Office 365 16.80 Universal深度解析与优化指南
  • CH57x蓝牙5.0开发实战:从机/主机模式与性能优化
  • 本地锁与分布式锁:Java本地锁为什么扛不住集群?
  • KiCad中高效使用AD封装库的完整指南
  • 5步掌握IINA:打造macOS专业级视频播放体验的完整指南
  • Claude与Codex协同开发:AI架构师与程序员组合实践
  • Codex 是 Agent 时代的操作系统底板:MCP 协议与 Skills 运行时解析