日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關咨詢
選擇下列產(chǎn)品馬上在線溝通
服務時間:8:30-17:00
你可能遇到了下面的問題
關閉右側工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
讓你瞬間提高工作效率的常用JS函數(shù)匯總

前言

本文總結了項目開發(fā)過程中常用的js函數(shù)和正則,意在提高大家平時的開發(fā)效率,具體內(nèi)容如下:

  1. 常用的正則校驗
  2. 常用的設備檢測方式
  3. 常用的日期時間函數(shù)
  4. 跨端事件處理
  5. js移動端適配方案
  6. xss預防方式
  7. 常用的js算法(防抖,截流,去重,排序,模板渲染,觀察者...)

代碼

1.正則

 
 
 
 
  1. // 匹配郵箱 
  2. let reg = /^([a-zA-Z]|[0-9])(\w|\-)+@[a-zA-Z0-9]+\.([a-zA-Z]{2,4})$ 
  3.  
  4. // (新)匹配手機號 
  5. let reg = /^1[0-9]{10}$/; 
  6.  
  7. // (舊)匹配手機號 
  8. let reg = /^1(3|4|5|7|8)[0-9]{9}$/; 
  9.  
  10. // 匹配8-16位數(shù)字和字母密碼的正則表達式 
  11. let reg = /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{8,16}$/; 
  12.  
  13. // 匹配國內(nèi)電話號碼 0510-4305211 
  14. let reg = /\d{3}-\d{8}|\d{4}-\d{7}/; 
  15.  
  16. // 匹配身份證號碼 
  17. let reg=/(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/; 
  18.  
  19. // 匹配騰訊QQ號 
  20. let reg = /[1-9][0-9]{4,}/; 
  21.  
  22. // 匹配ip地址 
  23. let reg = /\d+\.\d+\.\d+\.\d+/; 
  24.  
  25. // 匹配中文 
  26. let reg = /^[\u4e00-\u9fa5]*$/; 

2.檢測平臺(設備)類型

 
 
 
 
  1. let isWechat = /micromessenger/i.test(navigator.userAgent), 
  2.     isWeibo = /weibo/i.test(navigator.userAgent), 
  3.     isQQ = /qq\//i.test(navigator.userAgent), 
  4.     isIOS = /(iphone|ipod|ipad|ios)/i.test(navigator.userAgent), 
  5.     isAndroid = /android/i.test(navigator.userAgent); 

3.常用的日期時間函數(shù)

 
 
 
 
  1. // 時間格式化 
  2. function format_date(timeStamp) { 
  3.     let date = new Date(timeStamp); 
  4.     return date.getFullYear() + "年" 
  5.         + prefix_zero(date.getMonth() + 1) + "月" 
  6.         + prefix_zero(date.getDate()) + "日 " 
  7.         + prefix_zero(date.getHours()) + ":" 
  8.         + prefix_zero(date.getMinutes()); 
  9.  
  10. // 數(shù)字格式化 
  11. function prefix_zero(num) { 
  12.     return num >= 10 ? num : "0" + num; 
  13.  
  14. // 倒計時時間格式化 
  15. function format_time(timeStamp) { 
  16.     let day = Math.floor(timeStamp / (24 * 3600 * 1000)); 
  17.     let leave1 = timeStamp % (24 * 3600 * 1000); 
  18.     let hours = Math.floor(leave1 / (3600 * 1000)); 
  19.     let leave2 = leave1 % (3600 * 1000); 
  20.     let minutes = Math.floor(leave2 / (60 * 1000)); 
  21.     let leave3 = leave2 % (60 * 1000); 
  22.     let seconds = Math.floor(leave3 / 1000); 
  23.     if (day) return day + "天" + hours + "小時" + minutes + "分"; 
  24.     if (hours) return hours + "小時" + minutes + "分" + seconds + "秒"; 
  25.     if (minutes) return minutes + "分" + seconds + "秒"; 
  26.     if (seconds) return seconds + "秒"; 
  27.     return "時間到!"; 

5.跨端事件處理

 
 
 
 
  1. (function (doc, win) { 
  2.     var docEl = doc.documentElement, 
  3.         resizeEvt = 'orientationchange' in window ? 'orientationchange' : 'resize', 
  4.         recalc = function () { 
  5.             var clientWidth = docEl.clientWidth; 
  6.             var fontSize = 20; 
  7.             docEl.style.fontSize = fontSize + 'px'; 
  8.             var docStyles = getComputedStyle(docEl); 
  9.             var realFontSize = parseFloat(docStyles.fontSize); 
  10.             var scale = realFontSize / fontSize; 
  11.             console.log("realFontSize: " + realFontSize + ", scale: " + scale); 
  12.             fontSize = clientWidth / 667 * 20; 
  13.             if(isIphoneX()) fontSize = 19; 
  14.             fontSize = fontSize / scale; 
  15.             docEl.style.fontSize = fontSize + 'px'; 
  16.         }; 
  17.     // Abort if browser does not support addEventListener 
  18.     if (!doc.addEventListener) return; 
  19.     win.addEventListener(resizeEvt, recalc, false); 
  20.     doc.addEventListener('DOMContentLoaded', recalc, false); 
  21.  
  22.     // iphoneX判斷 
  23.     function isIphoneX(){ 
  24.         return /iphone/gi.test(navigator.userAgent) && (screen.height == 812 && screen.width == 375) 
  25.     } 
  26.  
  27. })(document, window); 

6.xss預防方式

 
 
 
 
  1. // 敏感符號轉義 
  2. function entities(s) { 
  3.     let e = { 
  4.         '"': '"', 
  5.         '&': '&', 
  6.         '<': '<', 
  7.         '>': '>' 
  8.     } 
  9.     return s.replace(/["<>&]/g, m => { 
  10.         return e[m] 
  11.     }) 

7.常用的js算法

 
 
 
 
  1. /** 
  2.  * 節(jié)流函數(shù)--規(guī)定在一個單位時間內(nèi),只能觸發(fā)一次函數(shù)。如果這個單位時間內(nèi)觸發(fā)多次函數(shù),只有一次生效。 
  3.  */ 
  4. function throttle(fun, delay) { 
  5.     let last, deferTimer 
  6.     return function (args) { 
  7.         let that = this 
  8.         let _args = arguments 
  9.         let now = +new Date() 
  10.         if (last && now < last + delay) { 
  11.             clearTimeout(deferTimer) 
  12.             deferTimer = setTimeout(function () { 
  13.                 last = now 
  14.                 fun.apply(that, _args) 
  15.             }, delay) 
  16.         }else { 
  17.             last = now 
  18.             fun.apply(that,_args) 
  19.         } 
  20.     } 
  21.  
  22. /** 
  23.  * 防抖函數(shù)--在事件被觸發(fā)n秒后再執(zhí)行回調,如果在這n秒內(nèi)又被觸發(fā),則重新計時 
  24.  */ 
  25.  function debounce(fun, delay) { 
  26.     return function (args) { 
  27.         let that = this 
  28.         clearTimeout(fun.id) 
  29.         fun.id = setTimeout(function () { 
  30.             fun.call(that, args) 
  31.         }, delay) 
  32.     } 
  33.  
  34. // 觀察者模式 
  35. let Observer = (function(){ 
  36.   let t __messages = {}; 
  37.   return { 
  38.     regist: function(type, fn) { 
  39.       if(typeof __messages[type] === 'undefined') { 
  40.         messages[type] = [fn]; 
  41.       }else { 
  42.         __messages[type].push(fn); 
  43.       } 
  44.     }, 
  45.     fire: function(type, args) { 
  46.       if(!__messages[type]){ 
  47.         return 
  48.       } 
  49.       let events = { 
  50.         type: type, 
  51.         args: args || {} 
  52.       }, 
  53.       i = 0, 
  54.       len = __messages[type].length; 
  55.       for(;i
  56.         __messages[type][i].call(this, events); 
  57.       } 
  58.     }, 
  59.     remove: function(type, fn) { 
  60.       if(__messages[type] instanceof Array){ 
  61.         let i = __messages[type].length -1; 
  62.         for(;i>=0;i--){ 
  63.           __messages[type][i] === fn && __messages[type].splice(i, 1) 
  64.         } 
  65.       } 
  66.     } 
  67.   } 
  68. })(); 
  69.  
  70.  // 模板渲染方法 
  71.  function formatString(str, data) { 
  72.    return str.replace(/\{\{(\w+)\}\}/g, function(match, key) { 
  73.      return typeof data[key] === undefined ? '' : data[key] 
  74.    }) 
  75.  } 
  76.   
  77.  // 冒泡排序 
  78. function bubbleSort(arr) { 
  79.     for (let i = arr.length - 1; i > 0; i--) { 
  80.       for (let j = 0; j < i; j++) { 
  81.         if (arr[j] > arr[j + 1]) { 
  82.           swap(arr, j, j + 1); 
  83.         } 
  84.       } 
  85.     } 
  86.     return arr; 
  87.  
  88. // 置換函數(shù) 
  89. function swap(arr, indexA, indexB) { 
  90.     [arr[indexA], arr[indexB]]= [arr[indexB], arr[indexA]]; 
  91.  
  92. // 數(shù)組去重 
  93. function distinct(arr = testArr) { 
  94.     return arr.filter((v, i, array) => array.indexOf(v) === i) 

后期會繼續(xù)總結更多工作中遇到的經(jīng)典函數(shù),也作為自己在工作中的一點總結。我們當然也可以直接使用lodash或ramda這些比較流行的函數(shù)式工具庫,在這里僅做學習參考使用。

本文轉載自微信公眾號「趣談前端」,可以通過以下二維碼關注。轉載本文請聯(lián)系趣談前端公眾號。


當前名稱:讓你瞬間提高工作效率的常用JS函數(shù)匯總
文章網(wǎng)址:http://www.dlmjj.cn/article/djsghee.html