13個(gè)JavaScript 一行程序,讓你看起來(lái)就是個(gè)專家
1. 獲得一個(gè)隨機(jī)的布爾值(true/false)
該函數(shù)使用Math.random()
方法返回一個(gè)布爾值(true
或者 false
)。Math.random創(chuàng)建一個(gè)0到1之間的隨機(jī)數(shù),我們只要檢查它是否高于或低于0.5,就有50%機(jī)會(huì)得到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)槭侵芪? console.log(isWeekday(new Date(2021, 7, 7))); // false 因?yàn)槭侵芰?
3.反轉(zhuǎn)字符串
有幾種不同的方法來(lái)反轉(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
(只讀屬性)返回布爾值,表示頁(yè)面是(true
)否(false
)隱藏。
const isBrowserTabInView = () => document.hidden; isBrowserTabInView();
場(chǎng)外:無(wú)意間發(fā)現(xiàn)愛(ài)奇藝廣告播放時(shí)間居然是在當(dāng)前標(biāo)簽頁(yè)激活的時(shí)候才會(huì)進(jìn)行倒計(jì)時(shí),離開(kāi)當(dāng)前標(biāo)簽頁(yè)的時(shí)候,倒計(jì)時(shí)停止,百度一下發(fā)現(xiàn)document.hidden
這個(gè)東東。
document.hidden
是h5新增加api使用的時(shí)候有兼容性問(wèn)題。
var hidden if (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)前頁(yè)面是否被隱藏:" + document[hidden])
5. 檢查一個(gè)數(shù)字是偶數(shù)還是奇數(shù)
const isEven = num => num % 2 === 0; console.log(isEven(2)); // true console.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.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. 檢查當(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)前瀏覽器是否在蘋(píng)果設(shè)備上
const isAppleDevice = /Mac|iPod|iPhone|iPad/.test(navigator.platform); console.log(isAppleDevice);
11. 滾動(dòng)到頁(yè)面頂部
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); // 59 celsiusToFahrenheit(0); // 32 celsiusToFahrenheit(-20); // -4 fahrenheitToCelsius(59); // 15 fahrenheitToCelsius(32); // 0
到此這篇關(guān)于13個(gè)JavaScript 一行程序,讓你看起來(lái)就是個(gè)專家的文章就介紹到這了,更多相關(guān)JavaScript 內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
前端通過(guò)JavaScript創(chuàng)建修改CAD圖形詳情
這篇文章介紹JavaScript創(chuàng)建修改CAD圖形,創(chuàng)建修改CAD圖形,一般是基于AutoCAD進(jìn)行二次開(kāi)發(fā),ObjectARX是AutoDesk公司針對(duì)AutoCAD平臺(tái)上的二次開(kāi)發(fā)而推出的一個(gè)開(kāi)發(fā)軟件包,它提供了以C++為基礎(chǔ)的面向?qū)ο蟮拈_(kāi)發(fā)環(huán)境及應(yīng)用程序接口,能真正快速的訪問(wèn)AutoCAD圖形數(shù)據(jù)庫(kù)2021-10-10微信小程序 本地存儲(chǔ)及登錄頁(yè)面處理實(shí)例詳解
這篇文章主要介紹了微信小程序 本地存儲(chǔ)實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-01-01微信小程序 location API接口詳解及實(shí)例代碼
這篇文章主要介紹了微信小程序 location API接口相關(guān)資料,這里詳細(xì)介紹了location API接口并附簡(jiǎn)單實(shí)例代碼,需要的朋友可以參考下2016-10-10ECharts框架Sunburst拖拽功能實(shí)現(xiàn)方案詳解
這篇文章主要為大家介紹了ECharts框架Sunburst拖拽功能實(shí)現(xiàn)方案詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12Web?Components實(shí)現(xiàn)類(lèi)Element?UI中的Card卡片
這篇文章主要為大家介紹了Web?Components實(shí)現(xiàn)類(lèi)Element?UI中的Card卡片實(shí)例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07微信小程序 animation API詳解及實(shí)例代碼
這篇文章主要介紹了 微信小程序 animation API詳解及實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2016-10-10js實(shí)現(xiàn)兔年轉(zhuǎn)圈圈動(dòng)畫(huà)示例
這篇文章主要為大家介紹了js實(shí)現(xiàn)兔年轉(zhuǎn)圈圈動(dòng)畫(huà)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01