常用js函数封装

深拷贝

export function type(obj) {
var toString = Object.prototype.toString
var map = {
'[object Boolean]': 'boolean',
'[object Number]': 'number',
'[object String]': 'string',
'[object Function]': 'function',
'[object Array]': 'array',
'[object Date]': 'date',
'[object RegExp]': 'regExp',
'[object Undefined]': 'undefined',
'[object Null]': 'null',
'[object Object]': 'object'
}
return map[toString.call(obj)]
} export function deepClone(data) {
var t = type(data); var o; var i; var ni if (t === 'array') {
o = []
} else if (t === 'object') {
o = {}
} else {
return data
} if (t === 'array') {
for (i = 0, ni = data.length; i < ni; i++) {
o.push(deepClone(data[i]))
}
return o
} else if (t === 'object') {
for (i in data) {
o[i] = deepClone(data[i])
}
return o
}
}

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;
}

截流防抖

function throttle(fn ,wait) {
let flag = false;
return function() {
if (flag) return;
flag = true;
fn.call(this, arguments);
setTimeout(() => {
flag = false;
}, wait);
}
} function debounce(fn, wait) {
let timer;
return function() {
if (timer) clearTimeout(timer);
timer = setTimeout(() => {
timer = null;
fn.call(this, arguments);
}, wait);
}
}
上一篇:【转】ACE开发环境搭建


下一篇:软工网络15团队作业8——Beta阶段敏捷冲刺