news 2026/3/31 3:20:41

超多JavaScript实用小妙招

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
超多JavaScript实用小妙招

字符串处理

1. 生成随机字符串

const randomString = (length = 8) => { return Math.random().toString(36).slice(2, 2 + length);};// 示例:randomString() → "4f9d2fe3"

2. 首字母大写

const capitalize = (str) => { return str.charAt(0).toUpperCase() + str.slice(1);};// 示例:capitalize("hello") → "Hello"

数组操作

3. 数组去重

const uniqueArray = (arr) => [...new Set(arr)];// 示例:uniqueArray([1,2,2,3]) → [1,2,3]

4. 数组乱序(洗牌算法)

const shuffleArray = (arr) => { return arr.sort(() => Math.random() - 0.5);};// 示例:shuffleArray([1,2,3,4]) → 随机顺序

对象处理

5. 深拷贝(简易版)

const deepClone = (obj) => JSON.parse(JSON.stringify(obj));// 注意:无法复制函数和循环引用

6. 对象属性过滤

const filterObject = (obj, keys) => { return Object.fromEntries( Object.entries(obj).filter(([key]) => keys.includes(key)) );};// 示例:filterObject({a:1, b:2}, ['a']) → {a:1}

数字处理

7. 千分位格式化

const formatNumber = (num) => { return num.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");};// 示例:formatNumber(1234567) → "1,234,567"

8. 生成范围随机数

const randomInRange = (min, max) => { return Math.floor(Math.random() * (max - min + 1)) + min;};// 示例:randomInRange(5, 10) → 5~10之间的整数

DOM 相关

9. 复制内容到剪贴板

const copyToClipboard = (text) => { navigator.clipboard.writeText(text);};

10. 检测元素是否可见

const isElementVisible = (el) => { return el.offsetParent !== null;};

日期处理

11. 格式化日期

const formatDate = (date = new Date(), format = 'YYYY-MM-DD') => { const pad = n => n.toString().padStart(2, '0'); return format .replace('YYYY', date.getFullYear()) .replace('MM', pad(date.getMonth() + 1)) .replace('DD', pad(date.getDate()));};// 示例:formatDate() → "2023-08-15"

函数优化

12. 防抖函数(Debounce)

const debounce = (fn, delay = 300) => { let timer; return (...args) => { clearTimeout(timer); timer = setTimeout(() => fn.apply(this, args), delay); };};// 适用场景:搜索框输入

13. 节流函数(Throttle)

const throttle = (fn, interval = 300) => { let lastTime = 0; return (...args) => { const now = Date.now(); if (now - lastTime >= interval) { fn.apply(this, args); lastTime = now; } };};// 适用场景:滚动事件

14. 类型判断增强版

const typeOf = (obj) => { return Object.prototype.toString.call(obj).slice(8, -1).toLowerCase();};// 示例:typeOf([]) → "array"

15. 本地存储封装

const storage = { set: (key, value) => localStorage.setItem(key, JSON.stringify(value)), get: (key) => { try { return JSON.parse(localStorage.getItem(key)); } catch (e) { return null; } }};

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/3/31 1:45:25

5个技巧打造专属系统:Windows10Debloater完全自定义指南

5个技巧打造专属系统:Windows10Debloater完全自定义指南 【免费下载链接】Windows10Debloater Sycnex/Windows10Debloater: 是一个用于Windows 10 的工具,可以轻松地卸载预装的应用和启用或禁用系统功能。适合对 Windows 10、系统优化和想要进行系统定制…

作者头像 李华
网站建设 2026/3/19 10:17:31

5个核心技能掌握Godot体素开发:从基础到高级应用

5个核心技能掌握Godot体素开发:从基础到高级应用 【免费下载链接】godot_voxel Voxel module for Godot Engine 项目地址: https://gitcode.com/gh_mirrors/go/godot_voxel Godot体素引擎为开发者提供了创建沉浸式3D世界的强大工具,通过自定义地形…

作者头像 李华
网站建设 2026/3/23 17:43:40

3步打造海尔智能家居系统:从传统家电到全屋智能的零门槛指南

3步打造海尔智能家居系统:从传统家电到全屋智能的零门槛指南 【免费下载链接】haier 项目地址: https://gitcode.com/gh_mirrors/ha/haier 一、价值定位:为什么普通用户也能玩转海尔智能家居? 你是否曾想过,不需要专业知…

作者头像 李华
网站建设 2026/3/24 18:23:37

BERTopic优化实战:5个行业验证的进阶技巧

BERTopic优化实战:5个行业验证的进阶技巧 【免费下载链接】BERTopic Leveraging BERT and c-TF-IDF to create easily interpretable topics. 项目地址: https://gitcode.com/gh_mirrors/be/BERTopic 在数据处理领域,BERTopic作为强大的主题建模…

作者头像 李华
网站建设 2026/3/22 17:42:44

如何探索GoldHEN作弊管理器的强大功能,解锁PS4游戏全新体验

如何探索GoldHEN作弊管理器的强大功能,解锁PS4游戏全新体验 【免费下载链接】GoldHEN_Cheat_Manager GoldHEN Cheats Manager 项目地址: https://gitcode.com/gh_mirrors/go/GoldHEN_Cheat_Manager 你是否曾遇到这样的困境:面对《血源诅咒》中令人…

作者头像 李华
网站建设 2026/3/27 21:18:57

3大突破!Motion Matching如何重构游戏动画逻辑

3大突破!Motion Matching如何重构游戏动画逻辑 【免费下载链接】MotionMatching Motion Matching implementation for Unity 项目地址: https://gitcode.com/gh_mirrors/mot/MotionMatching Motion Matching技术正引领游戏动画领域的范式转变,通过…

作者头像 李华