Javascript開發(fā)人員越來越多码俩,我們都希望代碼能夠更簡單,下面我們來搞幾個單行就能實(shí)現(xiàn)的騷操作歼捏。
01 隨機(jī)獲取布爾值
const getRandomBoolean = () => Math.random >= 0.5;
console.log(getRandomBoolean());
02 檢查日期是否為周末
const checkWeekend = (date) => [0,6].indexOf(date.getDay()) !== -1;
console.log(checkWeekend(new Date('2021-05-19')));
console.log(checkWeekend(new Date('2021-05-16')));
03 檢查奇稿存、偶數(shù)
const isEven = (num) => num % 2 === 0;
console.log(isEven(3));
04 獲取數(shù)組中的唯一值(數(shù)組去重)
const uniqueArr = (arr) => [...new Set(arr)];
console.log(uniqueArr([1, 2, 3, 1, 2, 3, 4, 5]));
05 在兩個數(shù)字之間生成一個隨機(jī)數(shù)
const random = (min,max) => Math.floor(Math.random(max - min + 1) + 1);
06 生成隨機(jī)字符串
const randomStr = () => Math.random().toString(36).slice(2);
07 滾動到頁面頂部
const scrollToTop = () => window.scrollTo(0,0);
08 交換兩個變量
[a,b] = [b,a]
09 計算兩個日期之間的天數(shù)
const daysDiff = (date0,date1) => Math.ceil(Math.abs(date0 - date1)/84600000);
10 復(fù)制到剪切板
const copyTextToClipboard = async (text) => {
await navigator.clipboard.writeText(text);
}
11 獲取數(shù)據(jù)類型
const trueTypeOf = (obj) => {
return Object.prototype.toString.call(obj).slice(8, -1).toLowerCase();
};
12 檢查當(dāng)前選項卡是否在視圖/焦點(diǎn)內(nèi)
const isTabInView = () => !document.hidden;
字符串反轉(zhuǎn)
let serverseStr = str.split("").severse().join("");
未完待續(xù)....