前端项目里面 js原生 公用方法

有时候想在自己的项目里面添加一些公用方法,用它们来做简单的一些代码操作,这样方便快捷

merge 对象合并,类似 jQuery 的extend

merge(target){
    for (let i = 1, j = arguments.length; i < j; i++) {
      let source = arguments[i] || {};
      for (let prop in source) {
        if (source.hasOwnProperty(prop)) {
          let value = source[prop];
          if (value !== undefined) {
            target[prop] = value;
          }
        }
      }
    }
    return target;
}
merge({},{a:1},{b:2}) //{a:1,b:2}

 

trim 去除字符串的两端的空格,这个常常在有表单的时候用到

getUrlParam 获取url后面的参数

urlEncode url地址参数拼接

 

trim(str){
    return str.replace(/(^\s*)|(\s*$)/g, "");
}

getUrlParam (name) {
    if (name) {
      var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
      var r = window.location.search.substr(1).match(reg);
      if (r != null) return unescape(r[2]);
      return null;
    } else {
      var urlParams;
      var match,
        pl     = /\+/g,  // Regex for replacing addition symbol with a space
        search = /([^&=]+)=?([^&]*)/g,
        decode = function (s) { return decodeURIComponent(s.replace(pl, " ")); },
        query  = window.location.search.substring(1);
        urlParams = {};
        while (match = search.exec(query))
           urlParams[decode(match[1])] = decode(match[2]);
      return urlParams;
    }
  }
//location.href : https://www.baidu.com/s?wd=cnblogs&rsv_spt=1
getUrlParam() // {wd:"cnblog",rsv_spt:1}
getUrlParam("wd") // cnblog

urlEncode(param, key, encode){
    if(param==null) return '';
    var paramStr = '';
    var t = typeof (param);
    if (t == 'string' || t == 'number' || t == 'boolean') {
      paramStr += '&' + key + '=' + ((encode==null||encode) ? encodeURIComponent(param) : param);
    } else {
      for (var i in param) {
        var k = key == null ? i : key + (param instanceof Array ? '[' + i + ']' : '.' + i);
        paramStr += Rxports.urlEncode(param[i], k, encode);
      }
    }
    return paramStr;
 }
urlEncode({a:1,b:2}) //www:xxx.com?a=1&b=2

  

简单js随机字符串生成

 

random(){
    return Math.random().toString(36).substr(2) + Date.now().toString(36);
}

 

 移动端匹配设备方法;主要根据navigator.userAgent 字符串 匹配来判断

 

var android = ua.match(/(Android);?[\s\/]+([\d.]+)?/);
var ipad = ua.match(/(iPad).*OS\s([\d_]+)/);
var ipod = ua.match(/(iPod)(.*OS\s([\d_]+))?/);
var iphone = !ipad && ua.match(/(iPhone\sOS)\s([\d_]+)/);

 

可以获取版本号

// Android
if (android) {
    device.os = 'android';
    device.osVersion = android[2];
    device.android = true;
    device.androidChrome = ua.toLowerCase().indexOf('chrome') >= 0;
}
if (ipad || iphone || ipod) {
    device.os = 'ios';
    device.ios = true;
}
// iOS
if (iphone && !ipod) {
    device.osVersion = iphone[2].replace(/_/g, '.');
    device.iphone = true;
}
if (ipad) {
    device.osVersion = ipad[2].replace(/_/g, '.');
    device.ipad = true;
}
if (ipod) {
    device.osVersion = ipod[3] ? ipod[3].replace(/_/g, '.') : null;
    device.iphone = true;
}

// iOS 8+ changed UA
if (device.ios && device.osVersion && ua.indexOf('Version/') >= 0) {
    if (device.osVersion.split('.')[0] === '10') {
        device.osVersion = ua.toLowerCase().split('version/')[1].split(' ')[0];
    }
}

// Webview
device.webView = (iphone || ipad || ipod) && ua.match(/.*AppleWebKit(?!.*Safari)/i);

 

 

 

  

 

posted @ 2017-04-28 10:23  含风billyang  阅读(297)  评论(0)    收藏  举报