15条あなたが知らないかもしれないJSの高い効率の技術


(JavaScriptに星印を付け、先端スキルを向上させる)
ミカンのカエデ車
https://blog.csdn.net/lj90019002/article/details/105364381
1、遅延関数delay
 const delay = ms => new Promise((resolve, reject) => setTimeout(resolve, ms))
 
 const getData = status => new Promise((resolve, reject) => {
     status ? resolve('done') : reject('fail')
 })
 const getRes = async (data) => {
     try {
         const res = await getData(data)
         const timestamp = new Date().getTime()
         await delay(1000)
         console.log(res, new Date().getTime() - timestamp)
     } catch (error) {
         console.log(error)
     }
 }
 getRes(true) //   1 
2、指定された長さの要素配列を分割します.
 const listChunk = (list, size = 1, cacheList = []) => {
     const tmp = [...list]
     if (size <= 0) {
         return cacheList
     }
     while (tmp.length) {
         cacheList.push(tmp.splice(0, size))
     }
     return cacheList
 }
 
 console.log(listChunk([1, 2, 3, 4, 5, 6, 7, 8, 9])) // [[1], [2], [3], [4], [5], [6], [7], [8], [9]]
 console.log(listChunk([1, 2, 3, 4, 5, 6, 7, 8, 9], 3)) // [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
 console.log(listChunk([1, 2, 3, 4, 5, 6, 7, 8, 9], 0)) // []
 console.log(listChunk([1, 2, 3, 4, 5, 6, 7, 8, 9], -1)) // []
3、行列の交叉を取得する
 const interp = (list, ...args) => list.filter(item => args.every(list => list.includes(item)))
 
 console.log(interp([2, 1], [2, 3])) // [2]
 console.log(interp([1, 2], [3, 4])) // []
4、関数コリック化
 const curring = fn => {
     const { length } = fn
     const curried = (...args) => {
         return (args.length >= length
               ? fn(...args)
               : (...args2) => curried(...args.concat(args2)))
     }
     return curried
 }
 
 const listMerge = (a, b, c) => [a, b, c]
 const curried = curring(listMerge)
 console.log(curried(1)(2)(3)) // [1, 2, 3]
 
 console.log(curried(1, 2)(3)) // [1, 2, 3]
 
 console.log(curried(1, 2, 3)) // [1, 2, 3]
5、文字列の前のスペースの削除と置換
 const trimStart = str => str.replace(new RegExp('^([\\s]*)(.*)$'), '$2')
 console.log(trimStart(' abc ')) // abc
 console.log(trimStart('123 ')) // 123
6、文字列の後ろのスペースの削除と置換
 const trimEnd = str => str.replace(new RegExp('^(.*?)([\\s]*)$'), '$1')
 console.log(trimEnd(' abc ')) //   abc
 console.log(trimEnd('123 ')) // 123
7、現在のサブ要素を取得するのは、親要素のサブ要素の順位です.
 const getIndex = el => {
     if (!el) {
         return -1
     }
     let index = 0
     do {
         index++
     } while (el = el.previousElementSibling);
     return index
 }
8、現在の要素のdocumentに対するオフセットを取得する
 const getOffset = el => {
     const {
         top,
         left
     } = el.getBoundingClientRect()
     const {
         scrollTop,
         scrollLeft
     } = document.body
     return {
         top: top + scrollTop,
         left: left + scrollLeft
     }
 }
9、要素タイプを取得する
const dataType = obj => Object.prototype.toString.call(obj).replace(/^\[object (.+)\]$/, '$1').toLowerCase();
10、移動端かどうかを判断する
 const isMobile = () => 'ontouchstart' in window
11、fadeアニメ
 const fade = (el, type = 'in') {
     el.style.opacity = (type === 'in' ? 0 : 1)
     let last = +new Date()
     const tick = () => {
         const opacityValue = (type === 'in' 
                             ? (new Date() - last) / 400
                             : -(new Date() - last) / 400)
         el.style.opacity = +el.style.opacity + opacityValue
         last = +new Date()
         if (type === 'in'
           ? (+el.style.opacity < 1)
           : (+el.style.opacity > 0)) {
             requestAnimationFrame(tick)
         }
     }
     tick()
 } 
12、指定された書式の文字列を日付文字列として解析します.
const dataPattern = (str, format = '-') => {
     if (!str) {
         return new Date()
     }
     const dateReg = new RegExp(`^(\\d{2})${format}(\\d{2})${format}(\\d{4})$`)
     const [, month, day, year] = dateReg.exec(str)
     return new Date(`${month}, ${day} ${year}`)
 }
 
 console.log(dataPattern('12-25-1995')) // Mon Dec 25 1995 00:00:00 GMT+0800 (      )
13、ホームページのコピーと貼り付けは禁止されています.
 const html = document.querySelector('html')
 html.oncopy = () => false
 html.onpaste = () => false
14、input枠の制限は中国語のみ入力できます.
 const input = document.querySelector('input[type="text"]')
 const clearText = target => {
     const {
         value
     } = target
     target.value = value.replace(/[^\u4e00-\u9fa5]/g, '')
 }
 input.onfocus = ({target}) => {
     clearText(target)
 }
 input.onkeyup = ({target}) => {
     clearText(target)
 }
 input.onblur = ({target}) => {
     clearText(target)
 }
 input.oninput = ({target}) => {
     clearText(target)
 }
15、文字列の中のhtmlコードを削除します.
 const removehtml = (str = '') => str.replace(/]*>/ig, '')
 console.log(removehtml('

')) //