JS开发经常使用工具函数

一、isStatic:检测数据是否是除了symbol外的原始数据javascript

function isStatic(value) {
    return (
        typeof value === 'string' ||
        typeof value === 'number' || 
        typeof value === 'boolean'  ||
        typeof value === 'undefined' ||
        value === null
    )
}

二、isPrimitive: 检测数据是否是原始数据java

function isPrimitive(value) {
    return isStatic(value) || typeof value === 'symbol'
}

3、isObject: 判断数据是否是引用类型的数据(例如:arrays,functions,objects,regexes,new Number(0),以及new String(‘’))android

function isObject(value) {
    let type = typeof value;
    return  value != null && (type == 'object' || type == 'function');
}

四、isObject:检查value是不是  类对象,若是一个值是类对象,那么它不该该是null,并且typeof后的结果是“object”ios

function isObjectLike(value) {
    return value != null && typeof value == 'object'
}

五、getRawType:获取数据类型,返回结果为 Number、String、Object、Arraychrome

function getRawType(value) {
    return Object.prototype.toString.call(value).slice(8,-1)
}

六、isPlainObject:判断数据是否是Object类型数据api

function isPlainObject(obj) {
    return Object.prototype.toString.call(obj) === '[object Object]'
}

七、isArray:判断数据是否是数组类型的数据数组

function isArray(arr) {
    return Object.prototype.toString.call(arr) === '[object Array]'
}

将isArray挂在到Array上
Array.isArray = Aray.isArray || isArray;

八、isRegExp:判断数据是否是正则对象,浏览器

function isRegExp(value) {
    return Object.prototype.toString.call(value) === '[object RegExp]'
}
isDate:判断是否是时间对象
function isDate(value) {
return Object.prototype.toString.call(value) === '[object Date]'
}

检查value是否是函数
Object.prototype.toStrig.call(value) === '[object Function]'

九、isNative:判断value是否是浏览器的内置函数缓存

     内置函数toString后的主体代码块为【native code】,而非内置函数则为相关代码,因此非内置函数能够进行拷贝(toString后起头去尾再由Function转)微信

function isNative(value) {
    return value === 'function' && /native code/.test(value.toString())
}

十、isLength:检查value是否为有效的类数组长度  

function isLength(value) {
    return typeof value == 'number' && value >-1 && value %1 == 0 && value <= Number.MAX_SAFE_INTEGER;
}

十一、isEmpty:检查value是否为空

若是是null,直接返回true,若是是类数组,判断数据长度;若是是Object,判断是否具备属性,若是是其余数据,直接返回false

funxtion isEmpty(value) {
    if(value == null) {
        return true;
    }
    if(isArrayLike(value)){
        return !value.length;
    }else if(isPlainObject(value)){
       for(let key in value) {
           if(hasOwnProperty.call(value,key)){
               return false;
          }
       }
    }
    return false;
}

十二、cached:记忆函数:缓存函数的运算结果

function cached(fn) {
    let cache = Object.create(null);
   return function cacheFn(str) {
       let hit = cache[str];
       return hit || (cache[str] = fn(str))
   }
}

1三、camelize:横线转驼峰命名

let camelizeRE = /-(\w)/g;
funtcion camelize(str) {
    return str.replace(camelizeRE,function(_,c){
       return c? c.toUpperCase() : '';
   })
}

1四、hyhenate:驼峰命名转横线命名:拆分字符串,使用-相连,并转为小写

let hyphenateRE = /\B(A-Z)/g;
function hyphenate(sr){
     return str.replace(hyphenateRE,'-$1').toLowerCase()
}

1五、capitalize:字符串首位大写

function capitalize(str) {
    return str.charAt(0).toUpperCase() +str.slice(1)
}

1六、识别各类浏览器及平台

let inBrowser = typeof window !== 'undefined';

//运行环境是微信
let inWeex = typeof WXEnvironment !== 'undefined' && !!WXEnvironment.platform;
let weexPlatform = inWeex && WXEnvironment.platform.toLowerCase();
//浏览器 UA 判断
let UA = inBrowser && window.navigator.userAgent.toLowerCase();
let isIE = UA && /msie|trident/.test(UA);
let isIE9 = UA && UA.indexOf('msie 9.0') > 0;
let isEdge = UA && UA.indexOf('edge/') > 0;
let isAndroid = (UA && UA.indexOf('android') > 0) || (weexPlatform === 'android');
let isIOS = (UA && /iphone|ipad|ipod|ios/.test(UA)) || (weexPlatform === 'ios');
let isChrome = UA && /chrome\/\d+/.test(UA) && !isEdge;
相关文章
相关标签/搜索