时间:2022-9-13 作者:悬浮的青春 分类: javascript
收集了 12 个在日常开发中非常常用的函数,有些可能很复杂,有些可能很简单,但我相信或多或少会对大家都会有所帮助。
你的网站是否需要生成随机颜色?下面一行代码就可以实现。
const generateRandomHexColor = () => `#${Math.floor(Math.random() * 0xffffff).toString(16)}`
console.log(generateRandomHexColor())
对数组的元素进行重新排序是一项非常重要的技巧,但是原生 Array 中并没有这项功能。
const shuffle = (arr) => arr.sort(() => Math.random() - 0.5)
const arr = [1, 2, 3, 4, 5]
console.log(shuffle(arr))
复制到剪切板是一项非常实用且能够提高用户便利性的功能。
const copyToClipboard = (text) => navigator.clipboard