自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

12 個(gè)JavaScript常用技巧,讓你看起來(lái)更像個(gè)專(zhuān)業(yè)人士

開(kāi)發(fā) 前端
Javascript可以做很多令人驚奇的事情,我也還有很多東西要學(xué),今天我們介紹12個(gè)簡(jiǎn)短而實(shí)用的代碼片段,幫助你提升工作效率。

Javascript可以做很多令人驚奇的事情,我也還有很多東西要學(xué),今天我們介紹12個(gè)簡(jiǎn)短而實(shí)用的代碼片段,幫助你提升工作效率。

1、判斷一個(gè)數(shù)是奇數(shù)還是偶數(shù)

模運(yùn)算符 % 做得很好。

const IsEven = num num % 2 === 0;console.log(IsEven(2));// Result: trueconsole.log(IsEven(3));// Result: false

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

檢查給定日期是否為工作日。

    const isWorkday = (date) => date.getDay() % 6 !== 0;    console.log(isWorkday(new Date("2022/10/17")));    // Result: true (Monday)    console.log(isWorkday(new Date("2022/10/16")));    // Result: false (Sumday)

3、獲取隨機(jī)布爾值(真/假)

使用 Math.random() 會(huì)返回一個(gè)介于 0 和 1 之間的隨機(jī)數(shù),然后判斷是否大于 0.5 會(huì)得到一個(gè)有 50% 概率為 True 或 False 的值。

const randomBool = () Math.random() >= 0.5;console.log(randomBool());

4、從日期對(duì)象獲取時(shí)間

使用 Date 對(duì)象的 .toTimeString() 方法將其轉(zhuǎn)換為時(shí)間字符串,然后截取該字符串。

const timeBeginDate = date date.toTimeString().slice(0, 8);    console.log(timeBeginDate(new Date(2022, 8, 10, 15, 30, 21)));    // Result: "15:30:21"    console.log(timeBeginDate(new Date()));    // Result: return current time

5、滾動(dòng)到頁(yè)面頂部

window.scrollTo() 會(huì)滾動(dòng)到指定坐標(biāo),如果坐標(biāo)設(shè)置為(0, 0),會(huì)返回到頁(yè)面頂部。

    const toTop = () window.scrollTo(0, 0);    toTop();

6、反轉(zhuǎn)字符串

反轉(zhuǎn)字符串的方法有很多,這里是最簡(jiǎn)單的一種,使用 split()、reverse() 和 join()

 const reverse = str str.split('').reverse().join('');
console.log(reverse('hello maxwell')); //Result: llewxam olleh

7、確定當(dāng)前選項(xiàng)卡是否可見(jiàn)

瀏覽器可以打開(kāi)很多標(biāo)簽,下面的代碼段是判斷當(dāng)前標(biāo)簽是否為活動(dòng)標(biāo)簽。

 const isBrowserTabInView = () document.hidden;    isBrowserTabInView();

8、檢查指定元素是否被聚焦

你可以使用 document.activeElement 來(lái)確定元素是否處于焦點(diǎn)中。

    const elementIsFocus = (el) => (el === document.activeElement);    elementIsFocus(anyElement)    // Returns True if it is in focus, otherwise returns False

9、判斷當(dāng)前用戶(hù)是否支持觸摸事件

const touchSupported = ()        ('ontouchstart' in window || window.DocumentTouch && document instanceof window.DocumentTouch);    }    console.log(touchSupported());    // Returns True if touch events are supported, otherwise returns False

10、判斷當(dāng)前用戶(hù)是否為 Apple 設(shè)備

你可以使用 navigator.platform 來(lái)確定當(dāng)前用戶(hù)是否是 Apple 設(shè)備。

const isAppleDevice = /Mac|iPod|iPhone|iPad/.test(navigator.platform);console.log(isAppleDevice);// If it is an Apple device it will return True otherwise it will return False

11、獲取所有參數(shù)的平均值

reduce() 函數(shù)可用于計(jì)算所有參數(shù)的平均值。

  const average = (...args) => args.reduce((a, b) =>    var avg = average(6,10, 8, 12);    console.log(avg);    // Result: 9

12、轉(zhuǎn)換華氏/攝氏度

不要再害怕處理溫度單位了,下面兩個(gè)函數(shù)就是兩個(gè)溫度單位的相互轉(zhuǎn)換。

  const celsiusToFahrenheit = (celsius) => celsius * 9 / 5 + 32;    const fahrenheitToCelsius = (fahrenheit) => (fahrenheit - 32) * 5 / 9;    // Examples    console.log(celsiusToFahrenheit(20));    // 68    console.log(celsiusToFahrenheit(0));    // 32    console.log(celsiusToFahrenheit(-15));    // 5    console.log(celsiusToFahrenheit(35));    // 95

寫(xiě)在最后

以上就是我今天跟你分享的全部?jī)?nèi)容,如果你覺(jué)得有用的話(huà),請(qǐng)記得點(diǎn)贊,關(guān)注我,我將與你分享更多實(shí)用的開(kāi)發(fā)技巧。


責(zé)任編輯:華軒 來(lái)源: web前端開(kāi)發(fā)
相關(guān)推薦

2023-08-01 14:36:00

JavaScript開(kāi)發(fā)

2024-08-27 15:25:33

2023-06-27 23:57:06

JavaScrip技能

2025-02-18 11:01:49

2025-03-17 10:42:12

2022-09-26 12:53:54

JavaScrip單行代碼

2023-07-11 15:43:16

JavaScript技巧

2024-09-13 16:19:47

2021-12-19 22:48:53

JavaScript開(kāi)發(fā)代碼

2012-04-11 09:44:42

谷歌Chrome OS

2022-06-21 14:30:16

Vim自定義Linux

2022-02-28 12:57:09

GNOMEPlasma桌面

2022-05-26 01:15:22

GitHub代碼快捷鍵

2022-02-21 12:05:49

LibreOffiLinux工具欄

2020-02-26 21:57:09

Lambdajava8方法引用

2020-06-29 15:00:31

UbuntumacOSLinux

2021-10-02 10:36:00

YAML編程語(yǔ)言軟件開(kāi)發(fā)

2013-12-30 10:06:51

智能硬件3D打印互聯(lián)網(wǎng)化

2016-08-01 11:33:40

云遷移云安全合規(guī)性

2024-03-08 12:20:25

Python代碼庫(kù)
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)