日韩欧美人妻无码精品白浆,夜夜嗨AV免费入口,国产欧美官网在线看,高校回应聋哑女生因长相完美被质疑

LOGO OA教程 ERP教程 模切知識(shí)交流 PMS教程 CRM教程 開(kāi)發(fā)文檔 其他文檔  
 
網(wǎng)站管理員

25 個(gè)殺手級(jí) JavaScript 單行代碼讓你看起來(lái)像個(gè)專業(yè)人士

admin
2024年10月13日 22:32 本文熱度 1051

?

你應(yīng)該知道的25個(gè)單行代碼片段,以提升你的 JavaScript 知識(shí)技能,同時(shí)幫助你提升工作效率。

那我們現(xiàn)在開(kāi)始吧。

1.將內(nèi)容復(fù)制到剪貼板

為了提高網(wǎng)站的用戶體驗(yàn),我們經(jīng)常需要將內(nèi)容復(fù)制到剪貼板,以便用戶將其粘貼到指定位置。

const copyToClipboard = (content) => navigator.clipboard.writeText(content)
copyToClipboard("Hello fatfish")

2.獲取鼠標(biāo)選擇

你以前遇到過(guò)這種情況嗎?

我們需要獲取用戶選擇的內(nèi)容。

const getSelectedText = () => window.getSelection().toString()
getSelectedText()

3.打亂數(shù)組

打亂數(shù)組?這在彩票程序中很常見(jiàn),但并不是真正隨機(jī)的。

const shuffleArray = array => array.sort(() => Math.random() - 0.5)
shuffleArray([ 1, 2,3,4, -1, 0 ]) // [3, 1, 0, 2, 4, -1]

4.將 rgba 轉(zhuǎn)換為十六進(jìn)制

我們可以將 rgba 和十六進(jìn)制顏色值相互轉(zhuǎn)換。

const rgbaToHex = (r, g, b) => "#" + [r, g, b].map(num => parseInt(num).toString(16).padStart(2, '0')).join('')
rgbaToHex(0, 0 ,0) // #000000rgbaToHex(255, 0, 127) //#ff007f

5.將十六進(jìn)制轉(zhuǎn)換為 rgba

const hexToRgba = hex => {  const [r, g, b] = hex.match(/\w\w/g).map(val => parseInt(val, 16))  return `rgba(${r}, ${g}, $, 1)`;}
hexToRgba('#000000') // rgba(0, 0, 0, 1)hexToRgba('#ff007f') // rgba(255, 0, 127, 1)

6.獲取多個(gè)數(shù)字的平均值

使用 reduce 我們可以非常方便地獲取一組數(shù)組的平均值。

const average = (...args) => args.reduce((a, b) => a + b, 0) / args.length
average(0, 1, 2, -1, 9, 10) // 3.5

7.檢查數(shù)字是偶數(shù)還是奇數(shù)

你如何判斷數(shù)字是奇數(shù)還是偶數(shù)?

const isEven = num => num % 2 === 0
isEven(2) // trueisEven(1) // false

8.刪除數(shù)組中的重復(fù)元素

要?jiǎng)h除數(shù)組中的重復(fù)元素,使用 Set 會(huì)變得非常容易。

const uniqueArray = (arr) => [...new Set(arr)]
uniqueArray([ 1, 1, 2, 3, 4, 5, -1, 0 ]) // [1, 2, 3, 4, 5, -1, 0]

9.檢查對(duì)象是否為空對(duì)象

判斷對(duì)象是否為空容易嗎?

const isEmpty = obj => Reflect.ownKeys(obj).length === 0 && obj.constructor === Object
isEmpty({}) // trueisEmpty({ name: 'fatfish' }) // false

10.反轉(zhuǎn)字符串

const reverseStr = str => str.split('').reverse().join('')
reverseStr('fatfish') // hsiftaf

11.計(jì)算兩個(gè)日期之間的間隔

const dayDiff = (d1, d2) => Math.ceil(Math.abs(d1.getTime() - d2.getTime()) / 86400000)
dayDiff(new Date("2023-06-23"), new Date("1997-05-31")) // 9519

12.找出日期所在的年份

const dayInYear = (d) => Math.floor((d - new Date(d.getFullYear(), 0, 0)) / 1000 / 60 / 60 / 24)
dayInYear(new Date('2023/06/23'))// 174

13.將字符串的首字母大寫(xiě)

const capitalize = str => str.charAt(0).toUpperCase() + str.slice(1)
capitalize("hello fatfish")  // Hello fatfish

14.生成指定長(zhǎng)度的隨機(jī)字符串

const generateRandomString = length => [...Array(length)].map(() => Math.random().toString(36)[2]).join('')
generateRandomString(12) // cysw0gfljoyxgenerateRandomString(12) // uoqaugnm8r4s

15.獲取兩個(gè)整數(shù)之間的隨機(jī)整數(shù)

const random = (min, max) => Math.floor(Math.random() * (max - min + 1) + min)
random(1, 100) // 27random(1, 100) // 84random(1, 100) // 55

16.指定數(shù)字四舍五入

const round = (n, d) => Number(Math.round(n + "e" + d) + "e-" + d)
round(3.1415926, 3) //3.142round(3.1415926, 1) //3.1

17.清除所有 cookie

const clearCookies = document.cookie.split(';').forEach(cookie => document.cookie = cookie.replace(/^ +/, '').replace(/=.*/, `=;expires=${new Date(0).toUTCString()};path=/`))

18.檢測(cè)是否為暗模式

const isDarkMode = window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches
console.log(isDarkMode)

19.滾動(dòng)到頁(yè)面頂部

const goToTop = () => window.scrollTo(0, 0)
goToTop()

20.確定是否為 Apple 設(shè)備

const isAppleDevice = () => /Mac|iPod|iPhone|iPad/.test(navigator.platform)
isAppleDevice()

21.隨機(jī)布爾值

const randomBoolean = () => Math.random() >= 0.5
randomBoolean()

22.獲取變量的類型

const typeOf = (obj) => Object.prototype.toString.call(obj).slice(8, -1).toLowerCase()
typeOf('')     // stringtypeOf(0)      // numbertypeOf()       // undefinedtypeOf(null)   // nulltypeOf({})     // objecttypeOf([])     // arraytypeOf(0)      // numbertypeOf(() => {})  // function

23.確定當(dāng)前選項(xiàng)卡是否處于活動(dòng)狀態(tài)

const checkTabInView = () => !document.hidden

24.檢查元素是否處于焦點(diǎn)

const isFocus = (ele) => ele === document.activeElement

25.隨機(jī) IP

const generateRandomIP = () => {  return Array.from({length: 4}, () => Math.floor(Math.random() * 256)).join('.');}
generateRandomIP() // 220.187.184.113generateRandomIP() // 254.24.179.151

總結(jié)

以上就是我今天與你分享的25個(gè)JS單行代碼片段,希望對(duì)你有所幫助。


該文章在 2024/10/14 11:08:36 編輯過(guò)
關(guān)鍵字查詢
相關(guān)文章
正在查詢...
點(diǎn)晴ERP是一款針對(duì)中小制造業(yè)的專業(yè)生產(chǎn)管理軟件系統(tǒng),系統(tǒng)成熟度和易用性得到了國(guó)內(nèi)大量中小企業(yè)的青睞。
點(diǎn)晴PMS碼頭管理系統(tǒng)主要針對(duì)港口碼頭集裝箱與散貨日常運(yùn)作、調(diào)度、堆場(chǎng)、車隊(duì)、財(cái)務(wù)費(fèi)用、相關(guān)報(bào)表等業(yè)務(wù)管理,結(jié)合碼頭的業(yè)務(wù)特點(diǎn),圍繞調(diào)度、堆場(chǎng)作業(yè)而開(kāi)發(fā)的。集技術(shù)的先進(jìn)性、管理的有效性于一體,是物流碼頭及其他港口類企業(yè)的高效ERP管理信息系統(tǒng)。
點(diǎn)晴WMS倉(cāng)儲(chǔ)管理系統(tǒng)提供了貨物產(chǎn)品管理,銷售管理,采購(gòu)管理,倉(cāng)儲(chǔ)管理,倉(cāng)庫(kù)管理,保質(zhì)期管理,貨位管理,庫(kù)位管理,生產(chǎn)管理,WMS管理系統(tǒng),標(biāo)簽打印,條形碼,二維碼管理,批號(hào)管理軟件。
點(diǎn)晴免費(fèi)OA是一款軟件和通用服務(wù)都免費(fèi),不限功能、不限時(shí)間、不限用戶的免費(fèi)OA協(xié)同辦公管理系統(tǒng)。
Copyright 2010-2025 ClickSun All Rights Reserved