zoukankan      html  css  js  c++  java
  • vue源码中的检测方法

    // 判断是否为undefined或null

    const isDef = (v) => {
      return v !== undefined && v !== null
    }

    // 判断是否为Promise 函数

    const isPromise = (val) => {
      return (
        val !== undefine &&
        typeof val.then === 'function' &&
        typeof val.catch === 'function'
      )
    }

    // 判断是否为简单数据类型

    const isPrimitive (value) => {
      return (
        typeof value === 'string' ||
        typeof value === 'number' ||
        typeof value === 'symbol' ||
        typeof value === 'boolean'
      )
    }

    // 严格检查复杂数据类型

    const isPlainObject = (obj) => {
      return Object.prototype.toString.call(obj) === '[object Object]'
    }
    
    const isRegExp = (v) => {
      return Object.prototype.toString.call(v) === '[object RegExp]'
    }

    // 将驼峰字符串转成连接符 magicEightTall 转换成 magic-eight-tall

    const hyphenateRE = /B([A-Z])/g
    const hyphenate = (str) => {
      return str.replace(hyphenateRE, '-$1').toLowerCase()
    }

    // 将连接符转成驼峰字符串 magic-eight-tall 转换成 magicEightTall

    const camelizeRE = /-(w)/g
    const camelize = (str) => {
        return str.replace(camelizeRE, (_, c) => c ? c.toUpperCase() : '')
    }

    资源搜索网站大全 https://www.renrenfan.com.cn 广州VI设计公司https://www.houdianzi.com

    // 如果不想重复转换,可用以下方法调用转换函数

    const cached = (fn) => {
        const cache = Object.create(null)
        console.log(cache);
        return ((str) => {
          const hit = cache[str]
          return hit || (cache[str] = fn(str))
        })
    };
    
    // 例
    const camelize = cached((str) => {
        return str.replace(camelizeRE, (_, c) => c ? c.toUpperCase() : '')
    })
  • 相关阅读:
    【数学】杜教筛
    【数学】【多项式】多项式求逆
    【数学】求导
    【数学】【多项式】快速数论变换(NTT)
    【数学】【多项式】快速傅里叶变换(FFT)
    WC2021 Day3 笔记
    网络流
    $DP$ 重修
    数学基础 5
    数学基础 3
  • 原文地址:https://www.cnblogs.com/xiaonian8/p/14092527.html
Copyright © 2011-2022 走看看