日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
15個有用的JavaScript技巧

今天這篇文章,是我從網(wǎng)絡(luò)上整理的一些常見的 JavaScript Tips。我在我的項(xiàng)目中使用了所有這些實(shí)用技巧,今天我想把它們分享給你,希望也能夠幫助到你。

1.數(shù)字分隔符

為了提高數(shù)字的可讀性,可以使用下劃線作為分隔符。

const largeNumber = 1_000_000_000;

console.log(largeNumber); // 1000000000

2.事件監(jiān)聽器只運(yùn)行一次

如果你想添加一個事件監(jiān)聽器并且只運(yùn)行一次,你可以使用 once 選項(xiàng)。

element.addEventListener('click', () => console.log('I run only once'), {
once: true
});

3. console.log變量包裝器

在 console.log() 中,將參數(shù)括在花括號中,以便您可以同時看到變量名和變量值。

 const name = "Maxwell";
console.log({ name });

4. 檢查 Caps Lock 是否打開

您可以使用 KeyboardEvent.getModifierState() 來檢測 Caps Lock 是否打開。

const passwordInput = document.getElementById('password');

passwordInput.addEventListener('keyup', function (event) {
if (event.getModifierState('CapsLock')) {
// CapsLock is open
}
});

5. 從數(shù)組中獲取最小值/最大值

您可以結(jié)合擴(kuò)展運(yùn)算符使用 Math.min() 或 Math.max() 來查找數(shù)組中的最小值或最大值。

const numbers = [5, 7, 1, 4, 9];

console.log(Math.max(...numbers)); // 9
console.log(Math.min(...numbers)); // 1

6.獲取鼠標(biāo)位置

您可以使用 MouseEvent 對象的 clientX 和 clientY 屬性的值來獲取有關(guān)當(dāng)前鼠標(biāo)位置坐標(biāo)的信息。

document.addEventListener('mousemove', (e) => {
console.log(`Mouse X: ${e.clientX}, Mouse Y: ${e.clientY}`);
});

7.復(fù)制到剪貼板

您可以使用剪貼板 API 創(chuàng)建“復(fù)制到剪貼板”功能。

function copyToClipboard(text) {
navigator.clipboard.writeText(text);
}

8.簡寫條件判斷語句

如果函數(shù)只在條件為真時才執(zhí)行,可以使用&&簡寫。

// Common writing method
if (condition) {
doSomething();
}

// Abbreviations
condition && doSomething();

9. console.table() 以特定格式打印表格

語法:

console.table(data [, columns]);

參數(shù):

data 表示要顯示的數(shù)據(jù)。它必須是數(shù)組或?qū)ο蟆?/p>

columns 表示包含列名稱的數(shù)組。

      function Person(firstName, lastName) {
this.firstName = firstName;
this.lastName = lastName;
}

const p1 = new Person("Mark", "Smith");
const p2 = new Person("Maxwell", "Siegrist");
const p3 = new Person("Lucy", "Jones");

console.table([p1, p2, p3], ["firstName"]);

10. 將字符串轉(zhuǎn)換為數(shù)字

const str = '508';

console.log(+str) // 508;

11.陣列去重

const numbers = [2, 3, 5, 5, 2];

console.log([...new Set(numbers)]); // [2, 3, 5]

12.過濾數(shù)組中的所有虛擬值

const myArray = [1, undefined, NaN, 2, null, '@maxwell', true, 5, false];

console.log(myArray.filter(Boolean)); // [1, 2, "@maxwell", true, 5]

13. include的用途

const myTech = 'JavaScript';
const techs = ['HTML', 'CSS', 'JavaScript'];

// Common writing method
if (myTech === 'HTML' || myTech === 'CSS' || myTech === 'JavaScript') {
// do something
}

// includes writing method
if (techs.includes(myTech)) {
// do something
}

14. 大量使用 reduce 求和數(shù)組

const myArray = [10, 20, 30, 40];
const reducer = (total, currentValue) => total + currentValue;

console.log(myArray.reduce(reducer)); // 100

15.元素的數(shù)據(jù)集

使用數(shù)據(jù)集屬性訪問元素的自定義數(shù)據(jù)屬性 (data-*)。


Hello Maxwell


總結(jié)

以上就是我今天想要跟你分享的關(guān)于JavaScript的技巧,如果你覺得這些技巧很棒的話,請記得點(diǎn)贊我,關(guān)注我,并將這篇文章分享給你的朋友們,也許能夠幫助到他。

感謝閱讀。


當(dāng)前名稱:15個有用的JavaScript技巧
網(wǎng)址分享:http://www.dlmjj.cn/article/ccsicoe.html