小工具函数

109 阅读1分钟
// inBrowser 检测当前宿主环境是否是浏览器
export const inBrowser = typeof window !== 'undefined'
// hasProto 检测当前环境是否可以使用对象的__proto__属性
export const hasProto = '__proto__' in {}
// 判断是否是原型类型
function isPrimitive (value) {
return (
typeof value === 'string' || typeof value === 'number' ||
typeof value === 'boolean' || typeof value === 'symbol'
)
}
// obj !== "null" && typeof obj === 'object'
// once只调用一次的函数
function once (fn) {
let flag = false;
return function(){
if (!flg) {
flag = true;
fn.apply(this, arguments)
}
}
}
// 创建缓存函数
function cached(fn) {
const cache = Object.create(null);
return function(str){
const hit = cache[str];
return hit || (cache[str] = fn(str))
}
}