13個JavaScript單行代碼,讓你看起來更專業(yè)

英文 |?https://medium.com/dailyjs/13-javascript-one-liners-thatll-make-you-look-like-a-pro-29a27b6f51cb
翻譯 | web前端開發(fā)公眾號(ID:web_qdkf)
JavaScript可以做很多令人驚奇的事情!
從復(fù)雜的框架到處理API,都需要學(xué)習(xí)很多東西。
但是,它也可以讓你僅用一行代碼就可以完成一些很棒的工作。
學(xué)習(xí)這13個JavaScript單行式代碼,讓你看起來更像專業(yè)人士!
1、隨機獲取布爾值(true/false)

此函數(shù)將使用Math.random()方法返回布爾值(真或假)。Math.random將創(chuàng)建一個介于0和1之間的隨機數(shù),然后我們檢查它是否大于或小于0.5。這意味著你有各50%的機會得到真或假值。
const randomBoolean = () => Math.random() >= 0.5;console.log(randomBoolean());// Result: a 50/50 change on returning true of false
2、判斷給的日期是否為工作日

使用此方法,你將可以判斷函數(shù)中提供的日期是工作日還是雙休日。
const isWeekday = (date) => date.getDay() % 6 !== 0;console.log(isWeekday(new Date(2021, 0, 11)));// Result: true (Monday)console.log(isWeekday(new Date(2021, 0, 10)));// Result: false (Sunday)
3、反轉(zhuǎn)字符串

用不同的方式可以反轉(zhuǎn)字符串。可以使用最簡單的split(),reverse()和join()方法。
const reverse = str => str.split('').reverse().join('');reverse('hello world');// Result: 'dlrow olleh'
4、判斷當(dāng)前選項卡是否在視圖/焦點中

我們可以使用document.hidden屬性檢查當(dāng)前標(biāo)簽頁是否在視圖/焦點中。
const isBrowserTabInView = () => document.hidden;isBrowserTabInView();// Result: returns true or false depending on if tab is in view / focus
5、判斷數(shù)字是偶數(shù)還是奇數(shù)

可以使用模運算符(%)解決的超簡單任務(wù)。如果你不太熟悉它,這是有關(guān)Stack Overflow的直觀說明(地址:https://stackoverflow.com/questions/17524673/understanding-the-modulus-operator/17525046#17525046)
const isEven = num => num % 2 === 0;console.log(isEven(2));// Result: trueconsole.log(isEven(3));// Result: false
6、從日期獲取時間

通過使用toTimeString()方法并將字符串切片用在正確的位置,我們可以從提供的日期中獲取時間,也可以獲取當(dāng)前時間。
const timeFromDate = date => date.toTimeString().slice(0, 8);console.log(timeFromDate(new Date(2021, 0, 10, 17, 30, 0)));// Result: "17:30:00"console.log(timeFromDate(new Date()));// Result: will log the current time
7、將數(shù)字四舍五入到固定的小數(shù)點

使用該Math.pow()方法,我們可以將數(shù)字四舍五入到函數(shù)中提供的某個小數(shù)點。
const toFixed = (n, fixed) => ~~(Math.pow(10, fixed) * n) / Math.pow(10, fixed);// ExamplestoFixed(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)前是否處于焦點

我們可以使用document.activeElement屬性檢查元素當(dāng)前是否處于焦點。
const elementIsInFocus = (el) => (el === document.activeElement);elementIsInFocus(anyElement)// Result: will return true if in focus, false if not in focus
9、檢查當(dāng)前用戶是否支持觸摸事件

const touchSupported = () => {('ontouchstart' in window || window.DocumentTouch && document instanceof window.DocumentTouch);}console.log(touchSupported());// Result: will return true if touch events are supported, false if not
10、檢查當(dāng)前用戶是否在Apple設(shè)備上

我們可以navigator.platform用來檢查當(dāng)前用戶是否在Apple設(shè)備上。
const isAppleDevice = /Mac|iPod|iPhone|iPad/.test(navigator.platform);console.log(isAppleDevice);// Result: will return true if user is on an Apple device
11、滾動到頁面頂部

window.scrollTo()方法將使用x和y坐標(biāo)滾動到。如果將它們設(shè)置為零和零,則將滾動到頁面頂部。
注意:Internet Explorer不支持該.scrollTo()方法。
const goToTop = () => window.scrollTo(0, 0);goToTop();// Result: will scroll the browser to the top of the page
12、獲取參數(shù)的平均值

我們可以使用reduce方法獲取在此函數(shù)中提供的參數(shù)的平均值。
const average = (...args) => args.reduce((a, b) => a + b) / args.length;average(1, 2, 3, 4);// Result: 2.5
13、轉(zhuǎn)換華氏/攝氏

最后一個是2合1!
應(yīng)對溫度有時會造成混亂。這兩個功能將幫助你將華氏溫度轉(zhuǎn)換為攝氏溫度,反之亦然。
const celsiusToFahrenheit = (celsius) => celsius * 9/5 + 32;const fahrenheitToCelsius = (fahrenheit) => (fahrenheit - 32) * 5/9;// ExamplescelsiusToFahrenheit(15); // 59celsiusToFahrenheit(0); // 32celsiusToFahrenheit(-20); // -4fahrenheitToCelsius(59); // 15fahrenheitToCelsius(32); // 0
謝謝閱讀!希望你今天學(xué)到了一些新知識。
本文完~

