util.js
Posted kankankankankan
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了util.js相关的知识,希望对你有一定的参考价值。
1 (function() { 2 var root = this, store, doc; 3 var _func = {}; 4 5 if(typeof module !== ‘undefined‘ && module.exports) { 6 module.exports = _func; 7 }else { 8 root._func = _func; 9 doc = root.document; 10 if((typeof Storage) !== ‘undefined‘) { 11 store = root.localStorage; 12 } 13 } 14 15 _func.log = function(type, text) { 16 swith(type) { 17 case ‘warn‘: 18 console.warn(text); 19 break; 20 case ‘log‘: 21 console.log(text); 22 break; 23 case ‘info‘: 24 console.info(text); 25 break; 26 case ‘error‘: 27 console.error(text); 28 break; 29 } 30 }; 31 32 _func.isArray = function(obj) { 33 return Object.prototype.toString.call(obj) === ‘[object Array]‘; 34 }; 35 36 _func.isFunction = function(obj) { 37 return Object.prototype.toString.call(obj) === ‘[object Function]‘; 38 }; 39 40 _func.isObject = function(obj) { 41 return Object.prototype.toString.call(obj) === ‘[object Object]‘; 42 }; 43 44 _func.isString = function(obj) { 45 return Object.prototype.toString.call(obj) === ‘[object String]‘; 46 }; 47 48 _func.isNumber = function(obj) { 49 return Object.prototype.toString.call(obj) === ‘[object Number]‘; 50 }; 51 52 _func.isBoolean = function(obj) { 53 return Object.prototype.toString.call(obj) === ‘[object Boolean]‘; 54 }; 55 56 _func.isDate = function(obj) { 57 return Object.prototype.toString.call(obj) === ‘[object Date]‘; 58 }; 59 60 _func.isNull = function(value) { 61 return value === ‘‘ || value === undefined || value === null ? true : false; 62 }; 63 64 _func.isEmpty =function (obj) { 65 var _func = this, flag = true; 66 67 if(_func.isArray(obj) || _func.isNumber(obj) || _func.isString(obj)){ 68 flag = obj.length === 0 ? true : false; 69 } 70 71 if(_func.isObject(obj)) { 72 for(var p in obj) { 73 if(obj.hasOwnProperty(p)) { 74 flag = false; 75 } 76 } 77 } 78 return flag; 79 }; 80 81 // 验证日期格式[yyyy-mm-dd] 82 _func.isDate = function(text) { 83 var reg = /^(?:(?!0000)[0-9]{4}-(?:(?:0[1-9]|1[0-2])-(?:0[1-9]|1[0-9]|2[0-8])|(?:0[13-9]|1[0-2])-(?:29|30)|(?:0[13578]|1[02])-31)|(?:[0-9]{2}(?:0[48]|[2468][048]|[13579][26])|(?:0[48]|[2468][048]|[13579][26])00)-02-29)$/; 84 return reg.test(text.toString()); 85 }; 86 87 // 验证邮箱 88 _func.isEmail = function(text) { 89 var reg = /([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)/; 90 return reg.test(text); 91 }; 92 93 // 验证ip 94 _func.isIp = function(text) { 95 var reg = /^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])((\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])){3}|(\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])){5})$/; 96 return reg.test(text); 97 }; 98 99 // 验证url 100 _func.isURL = function(text) { 101 var reg = /[a-zA-z]+:\/\/[^\s]/; 102 return reg.test(text); 103 }; 104 105 //验证账号或密码(字母开头,允许6-15字节,允许字母数字下划线) 106 _func.verifyAccount = function(text) { 107 var reg = /^[a-zA-Z][a-zA-Z0-9_]{5,14}$/; 108 return reg.test(text); 109 }; 110 111 // 去除字符串左右空格 112 _func.trim = function(text) { 113 return text.replace(/(^\s*)|(\s*$)/g, ""); 114 }; 115 116 //过滤字符串中的空格 117 _func.clearSpace = function(text) { 118 return text.replace(/[ ]/g, ""); 119 }; 120 121 // 检测字符串中是否包含中文 122 _func.existCN = function(text) { 123 var reg = /.*[\u4e00-\u9fa5]+.*$/; 124 return reg.test(text); 125 }; 126 127 //保留数字 128 _func.getNum = function(text) { 129 var regEx = /[^\d]/g; 130 return parseInt(text.replace(regEx, ‘‘)); 131 }; 132 133 //保留中文 134 _func.getCN = function(text) { 135 var regEx = /[^\u4e00-\u9fa5\uf900-\ufa2d]/g; 136 return text.replace(regEx, ‘‘); 137 }; 138 139 // 字符串截取 140 _func.trunc = function(text, length, separator) { 141 var _ = this, 142 len = length || text.length, 143 codes = _.isNull(separator) ? ‘...‘ : separator.toString(); 144 return (text.substring(0, len) + codes); 145 }; 146 147 //过滤html标签和  148 _func.excludeHTML = function(html){ 149 var regTag = /<\/?[^>]*>/g, 150 regSpeace = / /ig; 151 152 html = html.replace(regTag,‘‘); 153 html = html.replace(regSpeace,‘‘); 154 155 return html; 156 }; 157 158 // 生成范围随机数 159 _func.roundNum = function(start, end) { 160 return Math.floor(Math.random() * (end - start) + start); 161 }; 162 163 //随机验证码 [num:验证码位数] 164 _func.getCode = function(num) { 165 var arr1 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], 166 arr2 = [‘a‘, ‘b‘, ‘c‘, ‘d‘, ‘e‘, ‘f‘, ‘g‘, ‘h‘, ‘i‘, ‘j‘, ‘k‘, ‘l‘, ‘m‘, ‘n‘, ‘o‘, ‘p‘, ‘q‘, ‘r‘, ‘s‘, ‘t‘, ‘u‘, ‘v‘, ‘w‘, ‘x‘, ‘y‘, ‘z‘], 167 arr3 = [‘A‘, ‘B‘, ‘C‘, ‘D‘, ‘E‘, ‘F‘, ‘G‘, ‘H‘, ‘I‘, ‘J‘, ‘K‘, ‘L‘, ‘M‘, ‘N‘, ‘O‘, ‘P‘, ‘Q‘, ‘R‘, ‘S‘, ‘T‘, ‘U‘, ‘V‘, ‘W‘, ‘X‘, ‘Y‘, ‘Z‘], 168 arr = arr1.concat(arr2, arr3), 169 length = arr.length, 170 res = ‘‘; 171 172 for (var i = 0; i < num; i++) { 173 var charIndex = this.roundNum(0,length); 174 res += arr[charIndex]; 175 } 176 return res; 177 }; 178 179 180 _func.getDate = function(type) { 181 var time = new Date(), 182 year = time.getFullYear(), 183 m = time.getMonth() + 1, 184 month = m < 10 ? (‘0‘ + m) : m, 185 d = time.getDate(), 186 day = d < 10 ? (‘0‘ + d) : d, 187 dateText = ‘‘; 188 189 if (type === ‘CN‘) { 190 return dateText += year + ‘年‘ + month + ‘月‘ + day + ‘日‘; 191 } else { 192 return dateText += year + ‘-‘ + month + ‘-‘ + day; 193 } 194 }; 195 196 _func.getTimes = function(type) { 197 var time = new Date(), 198 hours = time.getHours(), 199 m = time.getMinutes(), 200 minutes = m < 10 ? (‘0‘ + m) : m, 201 s = time.getSeconds(), 202 seconds = s < 10 ? (‘0‘ + s) : s, 203 dateText = ‘‘; 204 205 if (type === ‘CN‘) { 206 return dateText += hours + ‘时‘ + minutes + ‘分‘ + seconds + ‘秒‘; 207 } else { 208 return dateText += hours + ‘:‘ + minutes + ‘:‘ + seconds; 209 } 210 }; 211 212 _func.getUrlParam = function() { 213 var reg_url = root.location.search, 214 reg_arr = [], 215 url_obj = {}; 216 217 if (reg_url.indexOf(‘&‘) !== -1) { 218 reg_arr = reg_url.substr(1).split(‘&‘); 219 for (var v in reg_arr) { 220 var key = reg_arr[v].split(‘=‘)[0], 221 value = reg_arr[v].split(‘=‘)[1]; 222 223 url_obj[key] = value; 224 } 225 return url_obj; 226 } else { 227 return reg_url.substr(1); 228 } 229 }; 230 231 //setInterval方法 232 _func.setTimesDo = function(callback, time, endTime, endCallback) { 233 var _func = this; 234 235 if (!_func.isNull(time) && !_func.isNull(callback)) { 236 if (endTime) { 237 var t = setInterval(function() { 238 callback(); 239 }, time); 240 _func.setTimeout = setTimeout(function() { 241 clearInterval(t); 242 if (endCallback) endCallback(); 243 }, time + endTime); 244 } else { 245 setInterval(callback, time); 246 } 247 } 248 }; 249 250 _func.setCookie = function(name, value, time) { 251 var d = new Date(), expires; 252 d.setTime(d.getTime() + (time * 1000)), 253 expires = "expires=" + d.toUTCString(); 254 doc.cookie = name + "=" + value + "; " + expires; 255 }; 256 257 _func.getCookie = function(cname) { 258 var name = cname + "="; 259 var ca = document.cookie.split(‘;‘); 260 for(var i=0; i<ca.length; i++) 261 { 262 var c = ca[i].trim(); 263 if (c.indexOf(name)==0) return c.substring(name.length,c.length); 264 } 265 return ""; 266 }; 267 268 _func.clearCookie = function(name) { 269 var _func = this; 270 _func.setCookie(name, "", -1); 271 }; 272 273 // 根据name设置localStorage本地存储[name相同,后面会覆盖前面的存储值] 274 _func.setStoreItem = function(name, val) { 275 var _func = this; 276 277 if (!_func.isNull(store)) { 278 store.setItem(name, val); 279 } 280 }; 281 282 283 // 根据name获取localStorage存储的值 284 _func.getStoreItem = function(name) { 285 var _func = this; 286 287 if (!_func.isNull(store)) { 288 return store.getItem(name); 289 } 290 }; 291 292 293 // 根据传入的object设置localStorage本地存储 294 _func.setStoreObj = function(obj) { 295 var _func = this; 296 if (_func.isObject(obj)) { 297 for (var v in obj) { 298 _func.setStoreItem(v, obj[v]); 299 } 300 } 301 }; 302 303 304 // 将所有localStorage本地存储以Object返回 305 _func.getStoreObj = function() { 306 var _func = this, 307 len = store.length, 308 obj = {}; 309 310 for (var i = 0; i < len; i++) { 311 var key = store.key(i), 312 val = _func.getStoreItem(key); 313 314 obj[key] = val; 315 } 316 317 return obj; 318 319 }; 320 321 322 // 根据name删除localStorage存储的值,成功则返回true 323 _func.removeStoreItem = function(name) { 324 var _func = this; 325 326 store.removeItem(name); 327 328 return _func.getStoreItem(name) === null ? true : false; 329 }; 330 331 332 // 清空localStorage的所有数据 333 _func.clearStore = function() { 334 store.clear(); 335 }; 336 337 _func.indexOf = function(arr, val) { 338 return arr.indexOf(val); 339 }; 340 341 //删除数组中指定的值 342 _func.delValue = function(arr, val) { 343 var _func = this; 344 for (var v in arr) { 345 if (arr[v] == val) arr.splice(_func.indexOf(arr, arr[v]), 1); 346 } 347 return arr; 348 }; 349 350 _func.unique = function(arr) { 351 var _func = this, 352 len = arr.length, 353 uq = {}, 354 outArr = [], 355 prefix = ‘‘; 356 357 for(var i=0; i<len; i++){ 358 if(_func.isString(arr[i])) { 359 prefix = ‘_str‘; 360 } else { 361 prefix = ‘‘; 362 } 363 if(!uq[arr[i]+prefix]){ 364 uq[arr[i]+prefix] = true; 365 outArr.push(arr[i]); 366 } 367 } 368 369 return outArr; 370 }; 371 372 //将数组转换成对象 373 //_func.ArratToObject(a,[‘leo‘,25]) => {‘name‘:‘leo‘,‘age‘:25,‘e-mail‘:undefined} 374 _func.ArratToObject = function(list, values) { 375 var _func = this, 376 len = list.length, 377 obj = {}; 378 379 if (_func.isArray(list) && _func.isArray(values)) { 380 for (var i = 0; i < len; i++) { 381 if (!values[i]) obj[list[i]] = undefined; 382 obj[list[i]] = values[i]; 383 } 384 } 385 386 return obj; 387 }; 388 389 //获取object对象所有的属性名称 390 _func.keys = function(obj) { 391 var _func = this, 392 outArr = []; 393 394 if (_func.isObject(obj)) { 395 for (var p in obj) { 396 if (_func.has(obj, p)) outArr.push(p); 397 } 398 } 399 return outArr; 400 }; 401 402 //返回object对象所有的属性值 403 _func.values = function(obj) { 404 var _func = this, 405 outArr = []; 406 407 if (_func.isObject(obj)) { 408 for (var v in obj) { 409 if (_func.has(obj, v)) outArr.push(obj[v]); 410 } 411 } 412 413 return outArr; 414 }; 415 416 //把一个对象转变为一个[key, value]形式的数组 417 _func.pairs = function(obj) { 418 var _func = this, 419 objArr = []; 420 421 if (_func.isObject(obj)) { 422 var keys = _func.keys(obj), 423 len = keys.length; 424 425 objArr.length = len; 426 427 for (var i = 0; i < len; i++) { 428 objArr[i] = [keys[i], obj[keys[i]]]; 429 } 430 } 431 return objArr; 432 }; 433 434 /** 435 * 将对象合并为一个对象返回 436 * 437 * _.assign({‘name‘:‘leo‘},{‘age‘:25}) => {‘name‘:‘leo‘,‘age‘:25} 438 * 439 */ 440 _func.assign = function(targetObj) { 441 var _func = this, 442 target = targetObj || {}, 443 objArr = arguments, 444 len = objArr.length; 445 446 for (var i = 1; i < len; i++) { 447 for (var v in objArr[i]) { 448 if (!_func.has(target, objArr[i][v])) { 449 target[v] = objArr[i][v]; 450 } 451 } 452 } 453 454 return target; 455 }; 456 }.call(this));
(function() {var root = this, store, doc;var _func = {};
if(typeof module !== ‘undefined‘ && module.exports) {module.exports = _func;}else {root._func = _func;doc = root.document;if((typeof Storage) !== ‘undefined‘) {store = root.localStorage;}}
_func.log = function(type, text) {swith(type) {case ‘warn‘:console.warn(text);break;case ‘log‘:console.log(text);break;case ‘info‘:console.info(text);break;case ‘error‘:console.error(text);break;}};
_func.isArray = function(obj) {return Object.prototype.toString.call(obj) === ‘[object Array]‘;};
_func.isFunction = function(obj) {return Object.prototype.toString.call(obj) === ‘[object Function]‘;};
_func.isObject = function(obj) {return Object.prototype.toString.call(obj) === ‘[object Object]‘;};
_func.isString = function(obj) {return Object.prototype.toString.call(obj) === ‘[object String]‘;};
_func.isNumber = function(obj) {return Object.prototype.toString.call(obj) === ‘[object Number]‘;};
_func.isBoolean = function(obj) {return Object.prototype.toString.call(obj) === ‘[object Boolean]‘;};
_func.isDate = function(obj) {return Object.prototype.toString.call(obj) === ‘[object Date]‘;};
_func.isNull = function(value) {return value === ‘‘ || value === undefined || value === null ? true : false;};
_func.isEmpty =function (obj) {var _func = this, flag = true;
if(_func.isArray(obj) || _func.isNumber(obj) || _func.isString(obj)){flag = obj.length === 0 ? true : false;}
if(_func.isObject(obj)) {for(var p in obj) {if(obj.hasOwnProperty(p)) {flag = false;}}}return flag;};
// 验证日期格式[yyyy-mm-dd]_func.isDate = function(text) {var reg = /^(?:(?!0000)[0-9]{4}-(?:(?:0[1-9]|1[0-2])-(?:0[1-9]|1[0-9]|2[0-8])|(?:0[13-9]|1[0-2])-(?:29|30)|(?:0[13578]|1[02])-31)|(?:[0-9]{2}(?:0[48]|[2468][048]|[13579][26])|(?:0[48]|[2468][048]|[13579][26])00)-02-29)$/;return reg.test(text.toString());};
// 验证邮箱 _func.isEmail = function(text) { var reg = /([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)/; return reg.test(text); };
// 验证ip _func.isIp = function(text) { var reg = /^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])((\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])){3}|(\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])){5})$/; return reg.test(text); };
// 验证url _func.isURL = function(text) { var reg = /[a-zA-z]+:\/\/[^\s]/; return reg.test(text); };
//验证账号或密码(字母开头,允许6-15字节,允许字母数字下划线) _func.verifyAccount = function(text) { var reg = /^[a-zA-Z][a-zA-Z0-9_]{5,14}$/; return reg.test(text); };
//去除字符串左右空格 _func.trim = function(text) { return text.replace(/(^\s*)|(\s*$)/g, ""); };
//过滤字符串中的空格 _func.clearSpace = function(text) { return text.replace(/[ ]/g, ""); };
// 检测字符串中是否包含中文 _func.existCN = function(text) { var reg = /.*[\u4e00-\u9fa5]+.*$/; return reg.test(text); };
//保留数字 _func.getNum = function(text) { var regEx = /[^\d]/g; return parseInt(text.replace(regEx, ‘‘)); };
//保留中文 _func.getCN = function(text) { var regEx = /[^\u4e00-\u9fa5\uf900-\ufa2d]/g; return text.replace(regEx, ‘‘); };
// 字符串截取 _func.trunc = function(text, length, separator) { var _ = this, len = length || text.length, codes = _.isNull(separator) ? ‘...‘ : separator.toString(); return (text.substring(0, len) + codes); };
//过滤HTML标签和  _func.excludeHTML = function(html){ var regTag = /<\/?[^>]*>/g, regSpeace = / /ig;
html = html.replace(regTag,‘‘); html = html.replace(regSpeace,‘‘);
return html; };
//生成范围随机数 _func.roundNum = function(start, end) { return Math.floor(Math.random() * (end - start) + start); };
//随机验证码 [num:验证码位数] _func.getCode = function(num) { var arr1 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], arr2 = [‘a‘, ‘b‘, ‘c‘, ‘d‘, ‘e‘, ‘f‘, ‘g‘, ‘h‘, ‘i‘, ‘j‘, ‘k‘, ‘l‘, ‘m‘, ‘n‘, ‘o‘, ‘p‘, ‘q‘, ‘r‘, ‘s‘, ‘t‘, ‘u‘, ‘v‘, ‘w‘, ‘x‘, ‘y‘, ‘z‘], arr3 = [‘A‘, ‘B‘, ‘C‘, ‘D‘, ‘E‘, ‘F‘, ‘G‘, ‘H‘, ‘I‘, ‘J‘, ‘K‘, ‘L‘, ‘M‘, ‘N‘, ‘O‘, ‘P‘, ‘Q‘, ‘R‘, ‘S‘, ‘T‘, ‘U‘, ‘V‘, ‘W‘, ‘X‘, ‘Y‘, ‘Z‘], arr = arr1.concat(arr2, arr3), length = arr.length, res = ‘‘;
for (var i = 0; i < num; i++) { var charIndex = this.roundNum(0,length); res += arr[charIndex]; } return res; };
_func.getDate = function(type) { var time = new Date(), year = time.getFullYear(), m = time.getMonth() + 1, month = m < 10 ? (‘0‘ + m) : m, d = time.getDate(), day = d < 10 ? (‘0‘ + d) : d, dateText = ‘‘;
if (type === ‘CN‘) { return dateText += year + ‘年‘ + month + ‘月‘ + day + ‘日‘; } else { return dateText += year + ‘-‘ + month + ‘-‘ + day; } };
_func.getTimes = function(type) { var time = new Date(), hours = time.getHours(), m = time.getMinutes(), minutes = m < 10 ? (‘0‘ + m) : m, s = time.getSeconds(), seconds = s < 10 ? (‘0‘ + s) : s, dateText = ‘‘;
if (type === ‘CN‘) { return dateText += hours + ‘时‘ + minutes + ‘分‘ + seconds + ‘秒‘; } else { return dateText += hours + ‘:‘ + minutes + ‘:‘ + seconds; } };
_func.getUrlParam = function() { var reg_url = root.location.search, reg_arr = [], url_obj = {};
if (reg_url.indexOf(‘&‘) !== -1) { reg_arr = reg_url.substr(1).split(‘&‘); for (var v in reg_arr) { var key = reg_arr[v].split(‘=‘)[0], value = reg_arr[v].split(‘=‘)[1];
url_obj[key] = value; } return url_obj; } else { return reg_url.substr(1); } };
//setInterval方法 _func.setTimesDo = function(callback, time, endTime, endCallback) { var _func = this;
if (!_func.isNull(time) && !_func.isNull(callback)) { if (endTime) { var t = setInterval(function() { callback(); }, time); _func.setTimeout = setTimeout(function() { clearInterval(t); if (endCallback) endCallback(); }, time + endTime); } else { setInterval(callback, time); } } };
_func.setCookie = function(name, value, time) { var d = new Date(), expires; d.setTime(d.getTime() + (time * 1000)), expires = "expires=" + d.toUTCString(); doc.cookie = name + "=" + value + "; " + expires; };
_func.getCookie = function(cname) { var name = cname + "="; var ca = document.cookie.split(‘;‘); for(var i=0; i<ca.length; i++) { var c = ca[i].trim(); if (c.indexOf(name)==0) return c.substring(name.length,c.length); } return ""; };
_func.clearCookie = function(name) { var _func = this; _func.setCookie(name, "", -1); };
// 根据name设置localStorage本地存储[name相同,后面会覆盖前面的存储值] _func.setStoreItem = function(name, val) { var _func = this;
if (!_func.isNull(store)) { store.setItem(name, val); } };
// 根据name获取localStorage存储的值 _func.getStoreItem = function(name) { var _func = this;
if (!_func.isNull(store)) { return store.getItem(name); } };
// 根据传入的object设置localStorage本地存储 _func.setStoreObj = function(obj) { var _func = this; if (_func.isObject(obj)) { for (var v in obj) { _func.setStoreItem(v, obj[v]); } } };
// 将所有localStorage本地存储以Object返回 _func.getStoreObj = function() { var _func = this, len = store.length, obj = {};
for (var i = 0; i < len; i++) { var key = store.key(i), val = _func.getStoreItem(key);
obj[key] = val; }
return obj;
};
// 根据name删除localStorage存储的值,成功则返回true _func.removeStoreItem = function(name) { var _func = this;
store.removeItem(name);
return _func.getStoreItem(name) === null ? true : false; };
// 清空localStorage的所有数据 _func.clearStore = function() { store.clear(); };
_func.indexOf = function(arr, val) { return arr.indexOf(val); };
//删除数组中指定的值 _func.delValue = function(arr, val) { var _func = this; for (var v in arr) { if (arr[v] == val) arr.splice(_func.indexOf(arr, arr[v]), 1); } return arr; };
_func.unique = function(arr) { var _func = this, len = arr.length, uq = {}, outArr = [], prefix = ‘‘;
for(var i=0; i<len; i++){ if(_func.isString(arr[i])) { prefix = ‘_str‘; } else { prefix = ‘‘; } if(!uq[arr[i]+prefix]){ uq[arr[i]+prefix] = true; outArr.push(arr[i]); } }
return outArr; };
//将数组转换成对象 //_func.ArratToObject(a,[‘leo‘,25]) => {‘name‘:‘leo‘,‘age‘:25,‘e-mail‘:undefined} _func.ArratToObject = function(list, values) { var _func = this, len = list.length, obj = {};
if (_func.isArray(list) && _func.isArray(values)) { for (var i = 0; i < len; i++) { if (!values[i]) obj[list[i]] = undefined; obj[list[i]] = values[i]; } }
return obj; };
//获取object对象所有的属性名称 _func.keys = function(obj) { var _func = this, outArr = [];
if (_func.isObject(obj)) { for (var p in obj) { if (_func.has(obj, p)) outArr.push(p); } } return outArr; };
//返回object对象所有的属性值 _func.values = function(obj) { var _func = this, outArr = [];
if (_func.isObject(obj)) { for (var v in obj) { if (_func.has(obj, v)) outArr.push(obj[v]); } }
return outArr; };
//把一个对象转变为一个[key, value]形式的数组 _func.pairs = function(obj) { var _func = this, objArr = [];
if (_func.isObject(obj)) { var keys = _func.keys(obj), len = keys.length;
objArr.length = len;
for (var i = 0; i < len; i++) { objArr[i] = [keys[i], obj[keys[i]]]; } } return objArr; };
/** * 将对象合并为一个对象返回 * * _.assign({‘name‘:‘leo‘},{‘age‘:25}) => {‘name‘:‘leo‘,‘age‘:25} * */ _func.assign = function(targetObj) { var _func = this, target = targetObj || {}, objArr = arguments, len = objArr.length;
for (var i = 1; i < len; i++) { for (var v in objArr[i]) { if (!_func.has(target, objArr[i][v])) { target[v] = objArr[i][v]; } } }
return target; };}.call(this));
以上是关于util.js的主要内容,如果未能解决你的问题,请参考以下文章
javascript cookie util #js #cookie