提升开发幸福感的10条JS技巧(JS,幸福感,技巧,web开发)

时间:2024-05-04 12:27:54 作者 : 石家庄SEO 分类 : web开发
  • TAG :

    %E6%8F%90%E5%8D%87%E5%BC%80%E5%8F%91%E5%B9%B8%E7%A6%8F%E6%84%9F%E7%9A%8410%E6%9D%A1JS%E6%8A%80%E5%B7%A7

{var length = 20var soupL...">

生成随机UID
const genUid = () => {
var length = 20
var soupLength = genUid.soup.length
var id = []
for (var i = 0; i < length; i++) {
id[i] = genUid.soup
.charAt(Math.random() soupLength)
}
return id.join('')
}
genUid.soup_ = '!#$%()
+,-./:;=?@[]^_{|}~ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'<br/>genUid() // ;lyCPc9A8IuK}?N6,%}
无loop生成指定长度的数组
const List = len => [...new Array(len).keys()]
const list = List(10) // [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
一行代码去重数组
const list = [1, 1, 2, 3, 6, 45, 8, 5, 4, 6, 5]
const uniqueList = [...new Set(list)] // [1, 2, 3, 6, 45, 8, 5, 4]
RGB色值生成16进制色值
const rgb2Hex = (r, g, b) => {
r = Math.max(Math.min(Number(r), 100), 0) 2.55
g = Math.max(Math.min(Number(g), 100), 0)
2.55
b = Math.max(Math.min(Number(b), 100), 0) 2.55
r = ('0' + (Math.round(r) || 0).toString(16)).slice(-2)
g = ('0' + (Math.round(g) || 0).toString(16)).slice(-2)
b = ('0' + (Math.round(b) || 0).toString(16)).slice(-2)
return '#' + r + g + b
}
rgb2Hex(100, 50, 0) // "#ff7f00"
颜色混合
const colourBlend = (c1, c2, ratio) => {
ratio = Math.max(Math.min(Number(ratio), 1), 0)
let r1 = parseInt(c1.substring(1, 3), 16)
let g1 = parseInt(c1.substring(3, 5), 16)
let b1 = parseInt(c1.substring(5, 7), 16)
let r2 = parseInt(c2.substring(1, 3), 16)
let g2 = parseInt(c2.substring(3, 5), 16)
let b2 = parseInt(c2.substring(5, 7), 16)
let r = Math.round(r1
(1 - ratio) + r2 ratio)
let g = Math.round(g1
(1 - ratio) + g2 ratio)
let b = Math.round(b1
(1 - ratio) + b2 * ratio)
r = ('0' + (r || 0).toString(16)).slice(-2)
g = ('0' + (g || 0).toString(16)).slice(-2)
b = ('0' + (b || 0).toString(16)).slice(-2)
return '#' + r + g + b
}
colourBlend('#ff0000', '#3333ff', 0.5) // "#991a80"
判断是否为质数
const mathIsPrime = n => {
if (n === 2 || n === 3) {
return true
}
if (isNaN(n) || n <= 1 || n % 1 != 0 || n % 2 == 0 || n % 3 == 0) {
return false;
}
for (let x = 6; x <= Math.sqrt(n) + 1; x += 6) {
if (n % (x - 1) == 0 || n % (x + 1) == 0) {
return false
}
}
return true
}
mathIsPrime(0) // true
遍历类数组对象
const elements = document.querySelectorAll(selector);
[].prototype.forEach.call(elements, (el, idx, list) => {
console.log(el) // 元素节点
})
判断对象类型
const type = data => Object.prototype.toString.call(data).replace(/^[object (.+)]$/, '$1').toLowerCase()
type({}) // object
优化多层判断条件
const getScore = score => {
const scoreData = new Array(101).fill(0)
.map((data, idx) => ([idx, () => (idx < 60 ? '不及格' : '及格')]))
const scoreMap = new Map(scoreData)
return (scoreMap.get(score)
? scoreMap.get(score)()
: '未知分数')
}
getScore(30) // 不及格
时间格式化
const dateFormatter = (formatter, date) => {
date = (date ? new Date(date) : new Date)
const Y = date.getFullYear() + '',
M = date.getMonth() + 1,
D = date.getDate(),
H = date.getHours(),
m = date.getMinutes(),
s = date.getSeconds()
return formatter.replace(/YYYY|yyyy/g, Y)
.replace(/YY|yy/g, Y.substr(2, 2))
.replace(/MM/g, (M < 10 ? '0' : '') + M)
.replace(/DD/g, (D < 10 ? '0' : '') + D)
.replace(/HH|hh/g, (H < 10 ? '0' : '') + H)
.replace(/mm/g, (m < 10 ? '0' : '') + m)
.replace(/ss/g, (s < 10 ? '0' : '') + s)
}

dateFormatter('YYYY-MM-DD HH:mm', '1995/02/15 13:55') // 1995-02-15 13:55
后记
如果此时正在看文章的你也有类似的技巧心得,不妨在下方留言来分享给大家。

如果觉得文章对您有帮助,请动动手指点赞加关注我哟!

本文:提升开发幸福感的10条JS技巧的详细内容,希望对您有所帮助,信息来源于网络。
上一篇:Dojo 中间件简介下一篇:

14 人围观 / 0 条评论 ↓快速评论↓

(必须)

(必须,保密)

阿狸1 阿狸2 阿狸3 阿狸4 阿狸5 阿狸6 阿狸7 阿狸8 阿狸9 阿狸10 阿狸11 阿狸12 阿狸13 阿狸14 阿狸15 阿狸16 阿狸17 阿狸18