vue源码中的检测方法的实现
判断是否为undefined或null
constisDef=(v)=>{
returnv!==undefined&&v!==null
}
判断是否为Promise函数
constisPromise=(val)=>{
return(
val!==undefine&&
typeofval.then==='function'&&
typeofval.catch==='function'
)
}
判断是否为简单数据类型
constisPrimitive(value)=>{
return(
typeofvalue==='string'||
typeofvalue==='number'||
typeofvalue==='symbol'||
typeofvalue==='boolean'
)
}
严格检查复杂数据类型
constisPlainObject=(obj)=>{
returnObject.prototype.toString.call(obj)==='[objectObject]'
}
constisRegExp=(v)=>{
returnObject.prototype.toString.call(v)==='[objectRegExp]'
}
将驼峰字符串转成连接符magicEightTall转换成magic-eight-tall
consthyphenateRE=/\B([A-Z])/g
consthyphenate=(str)=>{
returnstr.replace(hyphenateRE,'-$1').toLowerCase()
}
将连接符转成驼峰字符串magic-eight-tall转换成magicEightTall
constcamelizeRE=/-(\w)/g
constcamelize=(str)=>{
returnstr.replace(camelizeRE,(_,c)=>c?c.toUpperCase():'')
}
如果不想重复转换,可用以下方法调用转换函数
constcached=(fn)=>{
constcache=Object.create(null)
console.log(cache);
return((str)=>{
consthit=cache[str]
returnhit||(cache[str]=fn(str))
})
};
例
constcamelize=cached((str)=>{
returnstr.replace(camelizeRE,(_,c)=>c?c.toUpperCase():'')
})
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。