关于javascript:前端开发中经常使用的工具函数

45次阅读

共计 2340 个字符,预计需要花费 6 分钟才能阅读完成。

在日常开发中常常会用到的工具函数

URL 截取参数

// 间接调用输出想要截取的参数名称几个
export function getParamFromUrl(key) {if (key === undefined) return null;
    let search = location.search.substr(1);
    let mReg = new RegExp('(^|&)' + key + '=([^&]*)(&|$)');
    let mValue = search.match(mReg);
    if (mValue != null) return unescape(mValue[2]);
    return null;
}
// 示例
let city = getParamFromUrl('city');

JSON 是否为空判断

// 输出想要检测的 json 数据 如果为空返回返回 false
export function isNullObject(model) {if (typeof model === "object") {
    let hasProp = false;
    for (const prop in model) {
        hasProp = true;
        break;
    }
    if (hasProp) {return false;}
    return true;
  } else {throw "model is not object";}
}

数据类型检测

// 检测变量的数据类型
export function getParamType(item) {if (item === null) return null;
    if (item === undefined) return undefined;
    return Object.prototype.toString.call(item).slice(8, -1);
}
// 返回 String, Function, Boolean, Object, Number

获取 cookie

// 获取 document 下 cookie 的具体某个参数值
export function getCookie(key) {if (key === undefined) {return undefined;}
    let cookies = document.cookie;
    let mReg = new RegExp('(^|;)\\s*' + key + '=([^;]*)(;|$)');
    let mValue = cookies.match(mReg);
    let ret = undefined;
    if (mValue != null) {ret = unescape(mValue[2]);
    }
    if (ret !== undefined) {ret = ret.replace(/^\"|\'/i, '').replace(/\"|\'$/i,'');
    }
    return ret;
}

版本号比照

// 传入要比照的版本号,个别后面一个传入以后的版本号,前面一个写上要比照的版本号
export function versionCompare(higher, lower) {let sep = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : '.';

    let higherAry = higher.split(sep),
        lowerAry = lower.split(sep);
    let l = Math.max(higherAry.length, lowerAry.length);
    for (let i = 0; i < l; i++) {let high = parseInt(higherAry[i] || 0);
        let low = parseInt(lowerAry[i] || 0);
        if (high > low) {return 1;}
        if (high < low) {return -1;}
    }
    return 0;
}
// 返回值  higher>lower: 1;  higher=lower: 0;  higher<lower:-1

数组去重

export function arrayUniq(array){let temp = []; 
    for(var i = 0; i < array.length; i++){if(temp.indexOf(array[i]) == -1){temp.push(array[i]);
        }
    }
    return temp;
}

iPhone X 系列机型判断

export function isIphoneX() {
    // iPhone X、iPhone XS
    var isIPhoneX =
        /iphone/gi.test(window.navigator.userAgent) &&
        window.devicePixelRatio &&
        window.devicePixelRatio === 3 &&
        window.screen.width === 375 &&
        window.screen.height === 812;
    // iPhone XS Max
    var isIPhoneXSMax =
        /iphone/gi.test(window.navigator.userAgent) &&
        window.devicePixelRatio &&
        window.devicePixelRatio === 3 &&
        window.screen.width === 414 &&
        window.screen.height === 896;
    // iPhone XR
    var isIPhoneXR =
        /iphone/gi.test(window.navigator.userAgent) &&
        window.devicePixelRatio &&
        window.devicePixelRatio === 2 &&
        window.screen.width === 414 &&
        window.screen.height === 896;
    if (isIPhoneX || isIPhoneXSMax || isIPhoneXR) {return true;}
    return false;
}

正文完
 0