//返回一个值得数据类型
function type(a) {
return Object.prototype.toString.call(a)
}
//数组中根据对象的key去重
function uniqueArrHasObj(arr, objKey) {
var vessel = {}
var newData = arr.reduce(function (prev, cur, curIndex, curSelf) {
vessel[cur[objKey]] ? "" : vessel[cur[objKey]] = true && prev.push(cur)
return prev
}, [])
return newData
}
//当个数值数组去重
function uniqueIndexOf(arr) {
var newData = []
for (var i = 0; i < arr.length; i++) {
var item = arr[i]
var l = newData.indexOf(item)
if (l === -1) { newData.push(item) }
}
return newData
}
//当个数值数组去重数值或字符串
function uniqueObjKey(arr) {
var obj = {};
return arr.filter(function (ele) {
if (!obj[ele]) {
obj[ele] = true;
return true;
}
})
}
//深克隆(深克隆不考虑函数)
function deepClone(obj, result) {
var result = result || {};
for (var prop in obj) {
if (obj.hasOwnProperty(prop)) {
if (typeof obj[prop] == 'object' && obj[prop] !== null) {
if (Object.prototype.toString.call(obj[prop]) == '[object Object]') {
result[prop] = {};
}
else {
result[prop] = [];
}
deepClone(obj[prop], result[prop])
} else {
result[prop] = obj[prop]
}
}
}
return result;
}
//深克隆(除了函数)
function deepCloneExFn(arr) {
var res = JSON.parse(JSON.stringify(arr));
return res
}
//圣杯模式的继承
function inherit(Target, Origin) {
function F() { };
F.prototype = Origin.prototype;
Target.prototype = new F();
Target.prototype.constructor = Target;
Target.prop.uber = Origin.prototype;
}
//返回当前的时间(年月日时分秒)
function getDateTime() {
var date = new Date(), year = date.getFullYear(), month = date.getMonth() + 1, day = date.getDate(),
hour = date.getHours() + 1, minute = date.getMinutes(), second = date.getSeconds();
month = checkTime(month);
day = checkTime(day);
hour = checkTime(hour);
minute = checkTime(minute);
second = checkTime(second);
function checkTime(i) {
if (i < 10) {
i = "0" + i;
}
return i;
}
return "" + year + "年" + month + "月" + day + "日" + hour + "时" + minute + "分" + second + "秒"
}
//排序
function quickArr(arr) {
if (arr.length <= 1) {
return arr;
}
var left = [],
right = [];
var pIndex = Math.floor(arr.length / 2);
var p = arr.splice(pIndex, 1)[0];
for (var i = 0; i < arr.length; i++) {
if (arr[i] <= p) {
left.push(arr[i]);
} else {
right.push(arr[i]);
}
}
// 递归
return quickArr(left).concat([p], quickArr(right));
}
//防抖
function debounce(handle, delay) {
var timer = null;
return function () {
var _self = this,
_args = arguments;
clearTimeout(timer);
timer = setTimeout(function () {
handle.apply(_self, _args)
}, delay)
}
}
//节流
function throttle(handler, wait) {
var lastTime = 0;
return function (e) {
var nowTime = new Date().getTime();
if (nowTime - lastTime > wait) {
handler.apply(this, arguments);
lastTime = nowTime;
}
}
}
//邮箱验证的表达式
function isAvailableEmail(sEmail) {
var reg = /^([\w+\.])+@\w+([.]\w+)+$/
return reg.test(sEmail)
}
截取字符串地址
fz: function (stringTarget) {
var stringVal = stringTarget.slice(1).split('&')
var argument = {}
stringVal.forEach(function (item) {
item = item.split('=')
argument[item[0]] = item[1]
})
}