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;
相關文章
相關標籤/搜索