13個(gè)常用的?JavaScript代碼片段

1、獲得一個(gè)隨機(jī)的布爾值(true/false)
const randomBoolean = () => Math.random() >= 0.5;console.log(randomBoolean());
2、 檢查所提供的日期是否為工作日
使用這種方法,我們能夠檢查在函數(shù)中提供的日期是否是工作日或周末的日子。
const isWeekday = (date) => date.getDay() % 6 !== 0;console.log(isWeekday(new Date(2021, 7, 6)));// true 因?yàn)槭侵芪?/span>console.log(isWeekday(new Date(2021, 7, 7)));// false 因?yàn)槭侵芰?/span>
3、反轉(zhuǎn)字符串
有幾種不同的方法來反轉(zhuǎn)一個(gè)字符串。這是最簡(jiǎn)單的一種,使用split()、reverse()和join()方法。
const reverse = str => str.split('').reverse().join('');reverse('hello world');// 'dlrow olleh'
4、檢查當(dāng)前標(biāo)簽是否隱藏
Document.hidden (只讀屬性)返回布爾值,表示頁面是(true)否(false)隱藏。
const isBrowserTabInView = () => document.hidden;isBrowserTabInView();
場(chǎng)外:無意間發(fā)現(xiàn)愛奇藝廣告播放時(shí)間居然是在當(dāng)前標(biāo)簽頁激活的時(shí)候才會(huì)進(jìn)行倒計(jì)時(shí),離開當(dāng)前標(biāo)簽頁的時(shí)候,倒計(jì)時(shí)停止,百度一下發(fā)現(xiàn)document.hidden這個(gè)東東。
document.hidden是h5新增加api使用的時(shí)候有兼容性問題。
var hiddenif (typeof document.hidden !== "undefined") {hidden = "hidden";} else if (typeof document.mozHidden !== "undefined") {hidden = "mozHidden";} else if (typeof document.msHidden !== "undefined") {hidden = "msHidden";} else if (typeof document.webkitHidden !== "undefined") {hidden = "webkitHidden";}console.log("當(dāng)前頁面是否被隱藏:" + document[hidden])
5、檢查一個(gè)數(shù)字是偶數(shù)還是奇數(shù)
const isEven = num => num % 2 === 0;console.log(isEven(2));// trueconsole.log(isEven(3));// false
6、從一個(gè)日期獲取時(shí)間
const timeFromDate = date => date.toTimeString().slice(0, 8);console.log(timeFromDate(new Date(2021, 0, 10, 17, 30, 0)));// "17:30:00"console.log(timeFromDate(new Date()));// 打印當(dāng)前的時(shí)間
7、保留 n 位小數(shù)
const toFixed = (n, fixed) => ~~(Math.pow(10, fixed) * n) / Math.pow(10, fixed);// 事例toFixed(25.198726354, 1); // 25.1toFixed(25.198726354, 2); // 25.19toFixed(25.198726354, 3); // 25.198toFixed(25.198726354, 4); // 25.1987toFixed(25.198726354, 5); // 25.19872toFixed(25.198726354, 6); // 25.198726
8、檢查當(dāng)前是否有元素處于焦點(diǎn)中
我們可以使用document.activeElement屬性檢查一個(gè)元素是否當(dāng)前處于焦點(diǎn)。
const elementIsInFocus = (el) => (el === document.activeElement);elementIsInFocus(anyElement)// 如果在焦點(diǎn)中返回true,如果不在焦點(diǎn)中返回 false
9、檢查當(dāng)前瀏覽器是否支持觸摸事件
const touchSupported = () => {('ontouchstart' in window || window.DocumentTouch && document instanceof window.DocumentTouch);}console.log(touchSupported());// 如果支持觸摸事件,將返回true,如果不支持則返回false。
10、檢查當(dāng)前瀏覽器是否在蘋果設(shè)備上
const isAppleDevice = /Mac|iPod|iPhone|iPad/.test(navigator.platform);console.log(isAppleDevice);
11、滾動(dòng)到頁面頂部
const goToTop = () => window.scrollTo(0, 0);goToTop();
12、獲取參數(shù)的平均數(shù)值
const average = (...args) => args.reduce((a, b) => a + b) / args.length;average(1, 2, 3, 4);// 2.5
13、華氏/攝氏轉(zhuǎn)換
const celsiusToFahrenheit = (celsius) => celsius * 9/5 + 32;const fahrenheitToCelsius = (fahrenheit) => (fahrenheit - 32) * 5/9;// 事例celsiusToFahrenheit(15); // 59celsiusToFahrenheit(0); // 32celsiusToFahrenheit(-20); // -4fahrenheitToCelsius(59); // 15fahrenheitToCelsius(32); // 0
感謝你的閱讀。
學(xué)習(xí)更多技能
請(qǐng)點(diǎn)擊下方公眾號(hào)
![]()

評(píng)論
圖片
表情
