网络编程 
首页 > 网络编程 > 浏览文章

vue源码中的检测方法的实现

(编辑:jimmy 日期: 2024/5/8 浏览:3 次 )

判断是否为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 "htmlcode">
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() : '')
})

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

上一篇:对Layer UI 模块化的用法详解
下一篇:IntelliJ IDEA编辑器配置vue高亮显示
友情链接:杰晶网络 DDR爱好者之家 南强小屋 黑松山资源网 白云城资源网