zoukankan      html  css  js  c++  java
  • JS开发常用工具函数

    1、isStatic:检测数据是不是除了symbol外的原始数据

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

    2、isPrimitive: 检测数据是不是原始数据

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

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

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

    4、isObject:检查value是否是  类对象,如果一个值是类对象,那么它不应该是null,而且typeof后的结果是“object”

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

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

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

    6、isPlainObject:判断数据是不是Object类型数据

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

    7、isArray:判断数据是不是数组类型的数据

    function isArray(arr) {
        return Object.prototype.toString.call(arr) === '[object Array]'
    }
    
    将isArray挂在到Array上
    Array.isArray = Aray.isArray || isArray;
    

    8、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]'

    9、isNative:判断value是不是浏览器的内置函数

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

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

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

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

    11、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;
    }
    

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

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

    13、camelize:横线转驼峰命名

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

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

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

    15、capitalize:字符串首位大写

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

    16、识别各种浏览器及平台

    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;
    

      

     

     

     

     

     

  • 相关阅读:
    DAY1 linux 50条命令
    安卓2.0,3.0,4.0的差别
    java历史
    晶体管共射极单管放大电路
    jquery取消选择select下拉框
    oarcle数据库导入导出,创建表空间
    360chrome,google chrome浏览器使用jquery.ajax加载本地html文件
    jquery 选择器
    nodejs 相关
    关于http请求
  • 原文地址:https://www.cnblogs.com/lmxxlm-123/p/11190288.html
Copyright © 2011-2022 走看看