免费A级毛片无码专区网站-成人国产精品视频一区二区-啊 日出水了 用力乖乖在线-国产黑色丝袜在线观看下-天天操美女夜夜操美女-日韩网站在线观看中文字幕-AV高清hd片XXX国产-亚洲av中文字字幕乱码综合-搬开女人下面使劲插视频

JavaScript常用工具函數(shù)

  1. 檢測(cè)數(shù)據(jù)是不是除了symbol外的原始數(shù)據(jù)
function isStatic(value) {return (typeof value =https://www.huyubaike.com/biancheng/=='string' ||typeof value =https://www.huyubaike.com/biancheng/=='number' ||typeof value =https://www.huyubaike.com/biancheng/=='boolean' ||typeof value =https://www.huyubaike.com/biancheng/=='undefined' ||value =https://www.huyubaike.com/biancheng/== null)}
  1. 檢查數(shù)據(jù)是否為有效的類數(shù)組長(zhǎng)度
function isLength(value) {return (typeof value =https://www.huyubaike.com/biancheng/='number' &&value > -1 &&value % 1 == 0 &&value <= Number.MAX_SAFE_INTEGER)}
  1. 檢查數(shù)據(jù)是否為函數(shù)
function isFunction(value) {return Object.prototype.toString.call(value) === '[object Function]'}
  1. 判斷數(shù)據(jù)是否為時(shí)間對(duì)象
function isDate(value) {return Object.prototype.toString.call(value) === '[object Date]'}
  1. 判斷數(shù)據(jù)是否為正則對(duì)象
function isRegExp(value) {return Object.prototype.toString.call(value) === '[object RegExp]'}
  1. 判斷數(shù)據(jù)是否為數(shù)組類型的數(shù)據(jù)
function isArray(arr) {return Object.prototype.toString.call(arr) === '[object Array]'}
  1. 獲取數(shù)組中非undefined數(shù)據(jù)中的最大值
function max(arr) {arr = arr.filter(item => !_isNaN(item))return arr.length ? Math.max.apply(null, arr) : undefined}
  1. 獲取數(shù)組中非undefined數(shù)據(jù)中的最小值
function min(arr) {arr = arr.filter(item => !_isNaN(item))return arr.length ? Math.min.apply(null, arr) : undefined}
  1. 橫線轉(zhuǎn)駝峰命名
let camelizeRE = /-(\w)/gfunction camelize(str) {return str.replace(camelizeRE, function (_, c) {return c ? c.toUpperCase() : ''})}
  1. 駝峰命名轉(zhuǎn)橫線命名:拆分字符串,使用 - 相連,并且轉(zhuǎn)換為小寫
let hyphenateRE = /\B([A-Z])/gfunction hyphenate(str) {return str.replace(hyphenateRE, '-$1').toLowerCase()}
  1. 字符串首位大寫
function capitalize(str) {return str.charAt(0).toUpperCase() + str.slice(1)}
  1. 深拷貝
function deepCopy(obj, hash = new WeakMap()) {// 日期對(duì)象直接返回一個(gè)新的日期對(duì)象if (obj.constructor === Date) return new Date(obj)// 正則對(duì)象直接返回一個(gè)新的正則對(duì)象if (obj.constructor === RegExp) return new RegExp(obj)// 如果循環(huán)引用了就用WeakMap解決if (hash.has(obj)) return hash.get(obj)// 遍歷傳圖參數(shù)所有鍵的特性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. 數(shù)據(jù)深、淺拷貝
function deepCLone(obj) {// 定義變量檢測(cè)傳遞參數(shù)是對(duì)象還是數(shù)組let objClone = Array.isArray(obj) ? [] : {}if (obj && typeof obj === 'object' && obj != null) {// 判斷obju存在且類型為對(duì)象時(shí),因?yàn)閚ull也是for (let key in obj) {// 遍歷對(duì)象類型的obj,判斷obj中是否存在key屬性if (obj.hasOwnProperty(key)) {// 判斷如果obj[key]存在且是對(duì)象類型時(shí)應(yīng)進(jìn)行深拷貝,即在堆內(nèi)存中開(kāi)辟新的內(nèi)存if (obj[key] && typeof obj[key] === 'object') {// 遞歸實(shí)現(xiàn)深拷貝objClone[key] = deepCLone(obj[key])} else {// 淺拷貝objClone[key] = obj[key]}}}}return objClone}
  1. 獲取數(shù)據(jù)類型,返回結(jié)果為 Number、String、Object、Array等
function getRawType(value) {return Object.prototype.toString.call(value).slice(8, -1)}
  1. 格式化時(shí)間

    經(jīng)驗(yàn)總結(jié)擴(kuò)展閱讀