日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

13个JavaScript单行式代码

發布時間:2025/3/21 javascript 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 13个JavaScript单行式代码 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

13個JavaScript單行式代碼

1、隨機獲取布爾值(true/false)

此函數將使用Math.random()方法返回布爾值(真或假)。Math.random將創建一個介于0和1之間的隨機數,然后我們檢查它是否大于或小于0.5。這意味著你有各50%的機會得到真或假值。

const randomBoolean = () => Math.random() >= 0.5; console.log(randomBoolean()); // Result: a 50/50 change on returning true of false

2、判斷給的日期是否為工作日

使用此方法,你將可以判斷函數中提供的日期是工作日還是雙休日。

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、反轉字符串

用不同的方式可以反轉字符串。可以使用最簡單的split(),reverse()和join()方法。

const reverse = str => str.split('').reverse().join(''); reverse('hello world'); // Result: 'dlrow olleh'

4、判斷當前選項卡是否在視圖/焦點中

我們可以使用document.hidden屬性檢查當前標簽頁是否在視圖/焦點中。

const isBrowserTabInView = () => document.hidden; isBrowserTabInView(); // Result: returns true or false depending on if tab is in view / focus

5、判斷數字是偶數還是奇數

可以使用模運算符(%)解決的超簡單任務。如果你不太熟悉它,這是有關Stack Overflow的直觀說明(鏈接.)

const isEven = num => num % 2 === 0; console.log(isEven(2)); // Result: true console.log(isEven(3)); // Result: false

6、從日期獲取時間

通過使用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

7、將數字四舍五入到固定的小數點

使用該Math.pow()方法,我們可以將數字四舍五入到函數中提供的某個小數點。

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

8、檢查元素當前是否處于焦點

我們可以使用document.activeElement屬性檢查元素當前是否處于焦點。

const elementIsInFocus = (el) => (el === document.activeElement); elementIsInFocus(anyElement) // Result: will return true if in focus, false if not in focus

9、檢查當前用戶是否支持觸摸事件

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、檢查當前用戶是否在Apple設備上

我們可以navigator.platform用來檢查當前用戶是否在Apple設備上。

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坐標滾動到。如果將它們設置為零和零,則將滾動到頁面頂部。

注意:Internet Explorer不支持該.scrollTo()方法。

const goToTop = () => window.scrollTo(0, 0); goToTop(); // Result: will scroll the browser to the top of the page

12、獲取參數的平均值

我們可以使用reduce方法獲取在此函數中提供的參數的平均值。

const average = (...args) => args.reduce((a, b) => a + b) / args.length; average(1, 2, 3, 4); // Result: 2.5

13、轉換華氏/攝氏

最后一個是2合1!

應對溫度有時會造成混亂。這兩個功能將幫助你將華氏溫度轉換為攝氏溫度,反之亦然。

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 《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀

總結

以上是生活随笔為你收集整理的13个JavaScript单行式代码的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。