亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

13個(gè)JavaScript 一行程序,讓你看起來(lái)就是個(gè)專家

 更新時(shí)間:2021年09月15日 15:10:11   作者:前端小智  
JavaScript 可以做很多好玩的事, 從復(fù)雜的框架到處理API,有太多的東西需要學(xué)習(xí)。但是,它也能讓我們只用一行就能做一些了不起的事情。今天的文章小編就為大家介紹13 個(gè)JavaScript 行程序,需要的朋友可以參考下

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)文章

最新評(píng)論