Javascript 是一種腳本語言,用於建立動態更新的內容、控制多媒體、動畫圖像等等。javascript
使用 history.back()
能夠建立一個瀏覽器「返回」按鈕。html
<button onclick="history.back()">
返回
</button>
複製代碼
爲了提升數字的可讀性,您能夠使用下劃線做爲分隔符:java
const largeNumber = 1_000_000_000;
console.log(largeNumber); // 1000000000
複製代碼
若是你想添加一個事件監聽器而且只運行一次,你能夠使用 once
選項:數組
element.addEventListener('click', () => console.log('I run only once'), {
once: true
});
複製代碼
您在 console.log()
的時候,將參數用大括號括起來,這樣能夠同時看到變量名和變量值。瀏覽器
您能夠使用 Math.min()
或 Math.max()
結合擴展運算符來查找數組中的最小值或最大值。markdown
const numbers = [6, 8, 1, 3, 9];
console.log(Math.max(...numbers)); // 9
console.log(Math.min(...numbers)); // 1
複製代碼
您能夠使用 KeyboardEvent.getModifierState()
來檢測是否 Caps Lock
打開。函數
const passwordInput = document.getElementById('password');
passwordInput.addEventListener('keyup', function (event) {
if (event.getModifierState('CapsLock')) {
// CapsLock 已經打開了
}
});
複製代碼
您能夠使用 Clipboard
API 建立「複製到剪貼板」功能:oop
function copyToClipboard(text) {
navigator.clipboard.writeText(text);
}
複製代碼
您能夠使用 MouseEvent
對象下 clientX 和 clientY 的屬性值,獲取鼠標的當前位置座標信息。post
document.addEventListener('mousemove', (e) => {
console.log(`Mouse X: ${e.clientX}, Mouse Y: ${e.clientY}`);
});
複製代碼
您能夠設置 length 屬性來縮短數組。動畫
const numbers = [1, 2, 3, 4, 5]
numbers.length = 3;
console.log(numbers); // [1, 2, 3]
複製代碼
若是僅在判斷條件爲 true
時才執行函數,則能夠使用 &&
簡寫。
// 普通寫法
if (condition) {
doSomething();
}
// 簡寫
condition && doSomething();
複製代碼
語法:
// [] 裏面指的是可選參數
console.table(data [, columns]);
複製代碼
參數:
實例:
// 一個對象數組,只打印 firstName
function Person(firstName, lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
const john = new Person("John", "Smith");
const jane = new Person("Jane", "Doe");
const emily = new Person("Emily", "Jones");
console.table([john, jane, emily], ["firstName"]);
複製代碼
打印結果以下圖:
const numbers = [2, 3, 4, 4, 2];
console.log([...new Set(numbers)]); // [2, 3, 4]
複製代碼
const str = '404';
console.log(+str) // 404;
複製代碼
鏈接空字符串。
const myNumber = 403;
console.log(myNumber + ''); // '403'
複製代碼
const myArray = [1, undefined, NaN, 2, null, '@denicmarko', true, 3, false];
console.log(myArray.filter(Boolean)); // [1, 2, "@denicmarko", true, 3]
複製代碼
const myTech = 'JavaScript';
const techs = ['HTML', 'CSS', 'JavaScript'];
// 普通寫法
if (myTech === 'HTML' || myTech === 'CSS' || myTech === 'JavaScript') {
// do something
}
// includes 寫法
if (techs.includes(myTech)) {
// do something
}
複製代碼
const myArray = [10, 20, 30, 40];
const reducer = (total, currentValue) => total + currentValue;
console.log(myArray.reduce(reducer)); // 100
複製代碼
console.log()
樣式您知不知道能夠使用 CSS 語句在 DevTools 中設置 console.log
輸出的樣式:
使用 dataset
屬性訪問元素的自定義數據屬性 (data-*
):
<div id="user" data-name="John Doe" data-age="29" data-something="Some Data">
John Doe
</div>
<script> const user = document.getElementById('user'); console.log(user.dataset); // { name: "John Doe", age: "29", something: "Some Data" } console.log(user.dataset.name); // "John Doe" console.log(user.dataset.age); // "29" console.log(user.dataset.something); // "Some Data" </script>
複製代碼