• JavaScript常用工具函数


    检测数据是不是除了symbol外的原始数据
    function isStatic(value) {
      return (
        typeof value === 'string' ||
        typeof value === 'number' ||
        typeof value === 'boolean' ||
        typeof value === 'undefined' ||
        value === null
      )
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    检查数据是否为有效的类数组长度
    function isLength(value) {
      return (
        typeof value == 'number' &&
        value > -1 &&
        value % 1 == 0 &&
        value <= Number.MAX_SAFE_INTEGER
      )
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    检查数据是否为函数
    function isFunction(value) {
      return Object.prototype.toString.call(value) === '[object Function]'
    }
    
    • 1
    • 2
    • 3
    判断数据是否为时间对象
    function isDate(value) {
      return Object.prototype.toString.call(value) === '[object Date]'
    }
    
    • 1
    • 2
    • 3
    判断数据是否为正则对象
    function isRegExp(value) {
      return Object.prototype.toString.call(value) === '[object RegExp]'
    }
    
    • 1
    • 2
    • 3
    判断数据是否为数组类型的数据
    function isArray(arr) {
      return Object.prototype.toString.call(arr) === '[object Array]'
    }
    
    • 1
    • 2
    • 3
    获取数组中非undefined数据中的最大值
    function max(arr) {
      arr = arr.filter(item => !_isNaN(item))
      return arr.length ? Math.max.apply(null, arr) : undefined
    }
    
    • 1
    • 2
    • 3
    • 4
    获取数组中非undefined数据中的最小值
    function min(arr) {
      arr = arr.filter(item => !_isNaN(item))
      return arr.length ? Math.min.apply(null, arr) : undefined
    }
    
    • 1
    • 2
    • 3
    • 4
    横线转驼峰命名
    let camelizeRE = /-(\w)/g
    function camelize(str) {
      return str.replace(camelizeRE, function (_, c) {
        return c ? c.toUpperCase() : ''
      })
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    驼峰命名转横线命名:拆分字符串,使用 - 相连,并且转换为小写
    let hyphenateRE = /\B([A-Z])/g
    function hyphenate(str) {
      return str.replace(hyphenateRE, '-$1').toLowerCase()
    }
    
    • 1
    • 2
    • 3
    • 4
    字符串首位大写
    function capitalize(str) {
      return str.charAt(0).toUpperCase() + str.slice(1)
    }
    
    • 1
    • 2
    • 3
    深拷贝
    function deepCopy(obj, hash = new WeakMap()) {
      // 日期对象直接返回一个新的日期对象
      if (obj.constructor === Date) return new Date(obj)
      // 正则对象直接返回一个新的正则对象
      if (obj.constructor === RegExp) return new RegExp(obj)
      // 如果循环引用了就用WeakMap解决
      if (hash.has(obj)) return hash.get(obj)
    
      // 遍历传图参数所有键的特性
      let allDesc = Object.getOwnPropertyDescriptor(obj)
      // 继承原型链
      let copyObj = Object.create(Object.getPrototypeOf(obj), allDesc)
      hash.set(obj, copyObj)
      for (let key of Reflect.ownKeys(obj)) {
        copyObj[key] =
          isComplexDataType(obj[key]) && typeof obj[key] !== 'function'
            ? deepCopy(obj[key], hash)
            : obj[key]
      }
      function isComplexDataType(obj) {
        return (
          (typeof obj === 'object' || typeof obj === 'function') && obj !== null
        )
      }
      return copyObj
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    数据深、浅拷贝
    function deepCLone(obj) {
      // 定义变量检测传递参数是对象还是数组
      let objClone = Array.isArray(obj) ? [] : {}
    
      if (obj && typeof obj === 'object' && obj != null) {
        // 判断obju存在且类型为对象时,因为null也是
        for (let key in obj) {
          // 遍历对象类型的obj,判断obj中是否存在key属性
          if (obj.hasOwnProperty(key)) {
            // 判断如果obj[key]存在且是对象类型时应进行深拷贝,即在堆内存中开辟新的内存
            if (obj[key] && typeof obj[key] === 'object') {
              // 递归实现深拷贝
              objClone[key] = deepCLone(obj[key])
            } else {
              // 浅拷贝
              objClone[key] = obj[key]
            }
          }
        }
      }
      return objClone
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    获取数据类型,返回结果为 Number、String、Object、Array等
    function getRawType(value) {
      return Object.prototype.toString.call(value).slice(8, -1)
    }
    
    • 1
    • 2
    • 3
    实时获取当前时间
    function getTime(){
            var myDate = new Date();
            var myYear = myDate.getFullYear(); //获取完整的年份(4位,1970-????)
            var myMonth = myDate.getMonth()+1; //获取当前月份(0-11,0代表1月)
            var myToday = myDate.getDate(); //获取当前日(1-31)
            var myDay = myDate.getDay(); //获取当前星期X(0-6,0代表星期天)
            var myHour = myDate.getHours(); //获取当前小时数(0-23)
            var myMinute = myDate.getMinutes(); //获取当前分钟数(0-59)
            var mySecond = myDate.getSeconds(); //获取当前秒数(0-59)
            var week = ['星期日','星期一','星期二','星期三','星期四','星期五','星期六'];
            var nowTime;
    
            nowTime = myYear+'年'+fillZero(myMonth)+'月'+fillZero(myToday)+'日'+'  '+fillZero(myHour)+':'+fillZero(myMinute)+':'+fillZero(mySecond)+'  '+week[myDay]+'  ';
            //console.log(nowTime);
            // 页面渲染jq写法
            $('#time').html(nowTime);
        };
    // 每秒获取一次,实现实时更新
    setInterval(getTime, 1000)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    格式化时间
    /**
     * @name: dateFormater
     * @msg: 格式化时间
     * @param {*} formater 预期时间格式
     * @param {*} t 时间,可为空
     * @return {*}
     */
    function dateFormater(formater, t) {
      let date = t ? new Date(t) : new Date(),
        Y = date.getFullYear() + '',
        M = date.getMonth() + 1,
        D = date.getDate(),
        H = date.getHours(),
        m = date.getMinutes(),
        s = date.getSeconds()
      return formater
        .replace(/YYYY|yyyy/g, Y)
        .replace(/YY|yy/g, Y.substring(2, 2))
        .replace(/MM/g, (M < 10 ? '0' : '') + M)
        .replace(/DD/g, (D < 10 ? '0' : '') + D)
        .replace(/HH|hh/g, (H < 10 ? '0' : '') + H)
        .replace(/mm/g, (m < 10 ? '0' : '') + m)
        .replace(/ss/g, (s < 10 ? '0' : '') + s)
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    获取url参数,返回一个对象
    function GetUrlParam() {
      let url = document.location.toString()
      let arrObj = url.split('?')
      let params = Object.create(null)
      if (arrObj.length > 1) {
        arrObj = arrObj[1].split('&')
        arrObj.forEach(item => {
          item = item.split('=')
          params[item[0]] = item[1]
        })
      }
      return params
    }
    // ?a=1&b=2&c=3 ==> {a: "1", b: "2", c: "3"}
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    base64转blob
    /**
     * @desc base64转blob
     * @param {String} data base64
     * @return {Blob} blob
     */
    function dataURLtoBlob(data) {
      const dataAtob = atob(data)
      let dataLength = dataAtob.length
      const u8arr = new Uint8Array(dataLength)
      while (dataLength--) {
        u8arr[dataLength] = dataAtob.charCodeAt(dataLength)
      }
      return new Blob([u8arr])
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    #####. base64数据导出文件。文件下载

    function downloadFile(filename, data) {
      let DownloadLink = document.createElement('a')
    
      if (DownloadLink) {
        document.body.appendChild(DownloadLink)
        DownloadLink.style = 'display: none'
        DownloadLink.download = filename
        DownloadLink.href = data
    
        if (document.createEvent) {
          let DownloadEvt = document.createEvent('MouseEvents')
    
          DownloadEvt.initEvent('click', true, false)
          DownloadLink.dispatchEvent(DownloadEvt)
        } else if (document.createEventObject) DownloadLink.fireEvent('onclick')
        else if (typeof DownloadLink.onclick == 'function') DownloadLink.onclick()
    
        document.body.removeChild(DownloadLink)
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    下载文件
    /**
     * @desc 下载文件
     * @param {String} data 二进制流文件
     * @param {String} fileName 文件名
     * @param {boolean} transformBlob 是否是Blob,默认不传无需转换
     */
    export function download(data, fileName, transformBlob) {
      const blob = !transformBlob ? data : dataURLtoBlob(data)
    
      const blobUrl = window.URL.createObjectURL(blob)
    
      // desc:兼容IE浏览器blob文件下载
      if (navigator.msSaveOrOpenBlob) {
        navigator.msSaveOrOpenBlob(blob, fileName)
      } else {
        const a = document.createElement('a')
        a.style.display = 'none'
        a.href = blobUrl
        a.download = fileName
        document.body.appendChild(a)
        a.click()
    
        document.body.removeChild(a)
        window.URL.revokeObjectURL(blobUrl)
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    页面全屏
    function toFullScreen() {
      let el = document.documentElement
      let rfs =
        el.requestFullScreen ||
        el.webkitRequestFullScreen ||
        el.mozRequestFullScreen ||
        el.msRequestFullScreen
    
      //typeof rfs != "undefined" && rfs
      if (rfs) {
        rfs.call(el)
      } else if (typeof window.ActiveXObject !== 'undefined') {
        //for IE,这里其实就是模拟了按下键盘的F11,使浏览器全屏
        let wscript = new ActiveXObject('WScript.Shell')
        if (wscript != null) {
          wscript.SendKeys('{F11}')
        }
      } else {
        alert('浏览器不支持全屏')
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    退出全屏
    function exitFullscreen() {
      let el = parent.document
      let cfs =
        el.cancelFullScreen ||
        el.webkitCancelFullScreen ||
        el.mozCancelFullScreen ||
        el.exitFullScreen
    
      //typeof cfs != "undefined" && cfs
      if (cfs) {
        cfs.call(el)
      } else if (typeof window.ActiveXObject !== 'undefined') {
        //for IE,这里和fullScreen相同,模拟按下F11键退出全屏
        let wscript = new ActiveXObject('WScript.Shell')
        if (wscript != null) {
          wscript.SendKeys('{F11}')
        }
      } else {
        alert('切换失败,可尝试Esc退出')
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    利用performance.timing进行性能分析
    window.onload = function () {
      setTimeout(function () {
        let t = performance.timing
        console.log(
          'DNS查询耗时 :' + (t.domainLookupEnd - t.domainLookupStart).toFixed(0)
        )
        console.log('TCP链接耗时 :' + (t.connectEnd - t.connectStart).toFixed(0))
        console.log(
          'request请求耗时 :' + (t.responseEnd - t.responseStart).toFixed(0)
        )
        console.log(
          '解析dom树耗时 :' + (t.domComplete - t.domInteractive).toFixed(0)
        )
        console.log(
          '白屏时间 :' + (t.responseStart - t.navigationStart).toFixed(0)
        )
        console.log(
          'domready时间 :' +
            (t.domContentLoadedEventEnd - t.navigationStart).toFixed(0)
        )
        console.log(
          'onload时间 :' + (t.loadEventEnd - t.navigationStart).toFixed(0)
        )
    
        if ((t = performance.memory)) {
          console.log(
            'js内存使用占比 :' +
              ((t.usedJSHeapSize / t.totalJSHeapSize) * 100).toFixed(2) +
              '%'
          )
        }
      })
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    禁止某些键盘事件
    document.addEventListener('keydown', function (event) {
      return (
        !(
          (
            112 == event.code || //F1
            123 == event.code || //F12
            (event.ctrlKey && 82 == event.code) || //ctrl + R
            (event.ctrlKey && 78 == event.code) || //ctrl + N
            (event.shiftKey && 121 == event.code) || //shift + F10
            (event.altKey && 115 == event.code) || //alt + F4
            ('A' == event.srcElement.tagName && event.shiftKey)
          ) //shift + 点击a标签
        ) || (event.returnValue = false)
      )
    })
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    禁止右键、选择、赋值
    ['contextmenu', 'selectstart', 'copy'].forEach(function (ev) {
      document.addEventListener(ev, function (event) {
        return (event.returnValue = false)
      })
    })
    
    • 1
    • 2
    • 3
    • 4
    • 5

    常用js工具函数,长期维护更新

  • 相关阅读:
    【图神经网络论文整理】(三)—— HOW TO FIND YOUR FRIENDLY NEIGHBORHOOD:SuperGAT
    【Java】注解 之 定义注解
    Java 开发从零开始,java 基础入门传智播客网页版,Java 后端路线图
    电脑商城系统
    多分辨率时空深度学习模型:用于城市轨道交通动态OD需求预测
    四级单词大全h-n
    vue跨域简易版
    【0114】libpq连接句柄PGconn介绍
    R语言ggplot2可视化、使用geom_path函数可视化线图、geom_point函数在线图中添加散点标记、geom_text函数为每个散点添加标签信息
    ARM裸机二
  • 原文地址:https://blog.csdn.net/long321041080/article/details/127810355