Javascript可以做很多令人驚奇的事情,我也還有很多東西要學(xué),今天我們介紹12個(gè)簡(jiǎn)短而實(shí)用的代碼片段,幫助你提升工作效率。
Javascript可以做很多令人驚奇的事情,我也還有很多東西要學(xué),今天我們介紹12個(gè)簡(jiǎn)短而實(shí)用的代碼片段,幫助你提升工作效率。

1、判斷一個(gè)數(shù)是奇數(shù)還是偶數(shù)
模運(yùn)算符 % 做得很好。
const IsEven = num num % 2 === 0;console.log(IsEven(2));// Result: trueconsole.log(IsEven(3));// Result: false
2、判斷日期是否為工作日
檢查給定日期是否為工作日。
const isWorkday = (date) => date.getDay() % 6 !== 0; console.log(isWorkday(new Date("2022/10/17"))); // Result: true (Monday) console.log(isWorkday(new Date("2022/10/16"))); // Result: false (Sumday)
3、獲取隨機(jī)布爾值(真/假)
使用 Math.random() 會(huì)返回一個(gè)介于 0 和 1 之間的隨機(jī)數(shù),然后判斷是否大于 0.5 會(huì)得到一個(gè)有 50% 概率為 True 或 False 的值。
const randomBool = () Math.random() >= 0.5;console.log(randomBool());
4、從日期對(duì)象獲取時(shí)間
使用 Date 對(duì)象的 .toTimeString() 方法將其轉(zhuǎn)換為時(shí)間字符串,然后截取該字符串。
const timeBeginDate = date date.toTimeString().slice(0, 8); console.log(timeBeginDate(new Date(2022, 8, 10, 15, 30, 21))); // Result: "15:30:21" console.log(timeBeginDate(new Date())); // Result: return current time
5、滾動(dòng)到頁(yè)面頂部
window.scrollTo() 會(huì)滾動(dòng)到指定坐標(biāo),如果坐標(biāo)設(shè)置為(0, 0),會(huì)返回到頁(yè)面頂部。
const toTop = () window.scrollTo(0, 0); toTop();
6、反轉(zhuǎn)字符串
反轉(zhuǎn)字符串的方法有很多,這里是最簡(jiǎn)單的一種,使用 split()、reverse() 和 join()
const reverse = str str.split('').reverse().join('');
console.log(reverse('hello maxwell')); //Result: llewxam olleh
7、確定當(dāng)前選項(xiàng)卡是否可見(jiàn)
瀏覽器可以打開(kāi)很多標(biāo)簽,下面的代碼段是判斷當(dāng)前標(biāo)簽是否為活動(dòng)標(biāo)簽。
const isBrowserTabInView = () document.hidden; isBrowserTabInView();
8、檢查指定元素是否被聚焦
你可以使用 document.activeElement 來(lái)確定元素是否處于焦點(diǎn)中。
const elementIsFocus = (el) => (el === document.activeElement); elementIsFocus(anyElement) // Returns True if it is in focus, otherwise returns False
9、判斷當(dāng)前用戶(hù)是否支持觸摸事件
const touchSupported = () ('ontouchstart' in window || window.DocumentTouch && document instanceof window.DocumentTouch); } console.log(touchSupported()); // Returns True if touch events are supported, otherwise returns False
10、判斷當(dāng)前用戶(hù)是否為 Apple 設(shè)備
你可以使用 navigator.platform 來(lái)確定當(dāng)前用戶(hù)是否是 Apple 設(shè)備。
const isAppleDevice = /Mac|iPod|iPhone|iPad/.test(navigator.platform);console.log(isAppleDevice);// If it is an Apple device it will return True otherwise it will return False
11、獲取所有參數(shù)的平均值
reduce() 函數(shù)可用于計(jì)算所有參數(shù)的平均值。
const average = (...args) => args.reduce((a, b) => var avg = average(6,10, 8, 12); console.log(avg); // Result: 9
12、轉(zhuǎn)換華氏/攝氏度
不要再害怕處理溫度單位了,下面兩個(gè)函數(shù)就是兩個(gè)溫度單位的相互轉(zhuǎn)換。
const celsiusToFahrenheit = (celsius) => celsius * 9 / 5 + 32; const fahrenheitToCelsius = (fahrenheit) => (fahrenheit - 32) * 5 / 9; // Examples console.log(celsiusToFahrenheit(20)); // 68 console.log(celsiusToFahrenheit(0)); // 32 console.log(celsiusToFahrenheit(-15)); // 5 console.log(celsiusToFahrenheit(35)); // 95
寫(xiě)在最后
以上就是我今天跟你分享的全部?jī)?nèi)容,如果你覺(jué)得有用的話(huà),請(qǐng)記得點(diǎn)贊,關(guān)注我,我將與你分享更多實(shí)用的開(kāi)發(fā)技巧。