JavaScript能夠實現不少使人驚奇的事!前端
從複雜的框架處處理API,有太多的東西能夠學習。瀏覽器
甚至,僅用一行代碼,它也能完成一些很棒的工做。框架
不信?那麼請看這13條JavaScript單行代碼,用上它們,會讓你看起來特別專業!dom
使用Math.random()
方法可讓函數返回布爾值(true或false)。Math.random
會建立一個介於0和1之間的隨機數,而後咱們檢查這個數是大於仍是小於0.5。也就是說,有50%/50%的概率獲得true或false。編輯器
如下JS代碼塊顯示瞭如何使用Math.Random
方法獲取隨機布爾值。函數
const randomBoolean = () => Math.random() >= 0.5; console.log(randomBoolean()); // Result: a 50/50 change on returning true of false
咱們能夠經過此方法來檢查函數中提供的日期是工做日仍是週末。學習
如下JS代碼塊顯示瞭如何編寫一個返回指定日期是工做日仍是週末的函數。spa
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)
有兩種不一樣的方式能夠反轉字符串。使用split()
,reverse()
和join()
方法是最簡單的方法之一。插件
如下JS代碼塊顯示瞭如何反轉字符串。code
const reverse = str => str.split('').reverse().join(''); reverse('hello world'); // Result: 'dlrow olleh'
咱們可使用document.hidden
屬性來檢查當前tab是否在視圖/焦點中。
如下JS代碼塊顯示瞭如何使用文檔的hidden
屬性來獲取當前tab是否在視圖/焦點中。
const isBrowserTabInView = () => document.hidden; isBrowserTabInView(); // Result: returns true or false depending on if tab is in view / focus
能夠經過使用取模運算符(%
)來解決這個超簡單的任務。若是你對此還不太熟悉,那麼Stack Overflow上就有直觀的說明。
如下JS代碼塊顯示瞭如何使用模運算符來檢查數字是偶數仍是奇數。
const isEven = num => num % 2 === 0; console.log(isEven(2)); // Result: true console.log(isEven(3)); // Result: false
經過使用.toTimeString()
方法並在合適的位置切割字符串,咱們就能夠從指定的日期中獲取時間,也能夠獲取當前時間。
如下JS代碼塊顯示瞭如何經過toTimeString
方法和切割字符串從日期中獲取時間。
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
使用Math.pow()
方法,能夠截取數字小數點後的固定幾位。
如下JS代碼塊顯示瞭如何使用Math.Power
方法舍入某個小數點。
const toFixed = (n, fixed) => ~~(Math.pow(10, fixed) * n) / Math.pow(10, fixed); // Examples toFixed(25.198726354, 1); // 25.1 toFixed(25.198726354, 2); // 25.19 toFixed(25.198726354, 3); // 25.198 toFixed(25.198726354, 4); // 25.1987 toFixed(25.198726354, 5); // 25.19872 toFixed(25.198726354, 6); // 25.198726
咱們可使用document.activeElement
屬性來檢查一個元素當前是否處於焦點。
如下JS代碼塊顯示瞭如何使用文檔對象上的activeElement
屬性檢查元素當前是否處於焦點。
const elementIsInFocus = (el) => (el === document.activeElement); elementIsInFocus(anyElement) // Result: will return true if in focus, false if not in focus
如下JS代碼塊顯示瞭如何檢查當前用戶是否支持touch事件。
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
可使用navigator.platform
來檢查當前用戶是否使用Apple設備。
如下JS代碼塊顯示瞭如何檢查用戶當前是否在Apple設備上。
const isAppleDevice = /Mac|iPod|iPhone|iPad/.test(navigator.platform); console.log(isAppleDevice); // Result: will return true if user is on an Apple device
window.scrollTo()
方法經過x座標和y座標實現滾動。若是將它們設置爲0,則將滾動到頁面頂部。
注意:Internet Explorer不支持.scrollTo()
方法。
如下JS代碼塊顯示瞭如何使用scrollTo
方法將瀏覽器滾動至頂部。
const goToTop = () => window.scrollTo(0, 0); goToTop(); // Result: will scroll the browser to the top of the page
可使用reduce
方法獲取函數參數的平均值。
如下JS代碼塊顯示瞭如何使用reduce
方法獲取參數的平均值。
const average = (...args) => args.reduce((a, b) => a + b) / args.length; average(1, 2, 3, 4); // Result: 2.5
最後一個2合1了!
處理溫度有時會暈頭轉向。這兩個函數則能幫助你們將華氏溫度轉換爲攝氏溫度,以及將攝氏溫度轉換爲華氏溫度。
如下JS代碼塊顯示瞭如何將華氏溫度轉換爲攝氏溫度,以及反向的轉換。
const celsiusToFahrenheit = (celsius) => celsius * 9/5 + 32; const fahrenheitToCelsius = (fahrenheit) => (fahrenheit - 32) * 5/9; // Examples celsiusToFahrenheit(15); // 59 celsiusToFahrenheit(0); // 32 celsiusToFahrenheit(-20); // -4 fahrenheitToCelsius(59); // 15 fahrenheitToCelsius(32); // 0
文本完,感謝閱讀!
每日分享前端插件和前端開發教程,歡迎掃碼關注個人公衆號:前端新世界