Home >Web Front-end >JS Tutorial >js function encapsulation
This article shares with you the js function encapsulation. Friends who are interested can take a look
see them...
// 获取网址的get参数var GET = function(name) { var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i"); var r = window.location.search.substr(1).match(reg); if (r != null) return (r[2]); return null; }
// 13位时间戳转日期var getLocalTime = function(nS) { //return new Date(parseInt(nS)).toLocaleString().replace(/:\d{1,2}$/,' '); var date = new Date(nS); var Y = date.getFullYear() + '/'; var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '/'; var D = (date.getDate() < 10 ? '0' + (date.getDate()) : date.getDate()) + ' '; var h = date.getHours() + ':'; var m = (date.getMinutes() < 10 ? '0' + (date.getMinutes()) : date.getMinutes()) + ' '; //var s = date.getSeconds(); return Y + M + D + h + m; }
// 数组对象排序 data.sort(keysrt("firstWord"));var keysrt = function(propertyName) { return function(object1, object2) { var value1 = object1[propertyName]; var value2 = object2[propertyName]; if (value2 < value1) { return 1; } else if (value2 > value1) { return -1; } else { return 0; } } }
// 判断数据类型function type(elem) { if (elem == null) { return elem + ''; } return toString.call(elem).replace(/[\[\]]/g, '').split(' ')[1].toLowerCase(); }
// 判断数组元素是否重复isArrRepeat(arr) { var _arr = arr.sort(); console.log(_arr) for (var i = 0; i < _arr.length; i++) { if (_arr[i] === _arr[i + 1]) { return true; } } return false; }
Related recommendations:
The above is the detailed content of js function encapsulation. For more information, please follow other related articles on the PHP Chinese website!