JS笔记

75 阅读1分钟

1.js空值合并运算符:

const temp = (undefined || null) ?? 'value'; // 'value';

2.js数组实例at方法:

// Array.prototype.at(index)

const arr = [1, 2, 3, 4, 5];
arr.at(0); // 1
arr.at(1); // 2
arr.at(5); // undefined

arr.at(-1); // 5
arr.at(-2); // 4
arr.at(-5); // 1
arr.at(-6); // undefined

// 相当于 arr[index>=0 ? index : arr.length+index]

3.判断元素是否有某个样式

Element.matches()

<div id="test" class="aa bb"></div>

const d = document.getElementById('test');
d.matches('.aa') // true
d.matches('.cc') // false