1. 获取url中传参的值
function getParam (name) {
var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i');
var r=window.location.search.substr(1).replace(new RegExp(/(amp;)/g),'').match(reg);
if (r != null) {
return r[2];
}
return null;
};
2. 判断变量的数据类型
function typeOf(obj) {
const toString = Object.prototype.toString;
const 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)];
},
3. 深度克隆对象或数组,多个对象或数组嵌套
deepCopy(data) {
const t = this.typeOf(data);
let o;
if (t === 'array') {
o = [];
} else if ( t === 'object') {
o = {};
} else {
return data;
}
if (t === 'array') {
for (let i = 0; i < data.length; i++) {
o.push(this.deepCopy(data[i]));
}
} else if ( t === 'object') {
for (let i in data) {
o[i] = this.deepCopy(data[i]);
}
}
return o;
},
4. 节流函数
debounce(func, delay) {
let timer;
return function (...args) {
if (timer) {
clearTimeout(timer);
}
timer = window.setTimeout(() => {
func.apply(this, args);
}, delay);
}
},
5. 生成随机字符串
function randomStr (len) {
len = len || 32;
var chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
var maxPos = chars.length;
var pwd = '';
for (let i = 0; i < len; i++) {
pwd += chars.charAt(Math.floor(Math.random() * maxPos));
}
return pwd;
};
6. 判断对象是否为空
function isEmptyObject (obj) {
var name;
for (name in obj) {
return false;
}
return true;
},
7. 判断是否有该class
export function hasClass(el, className){
let reg = new RegExp('(^|\\S)'+className+'(\\s|$)');
return reg.test(el.className);
}
8. 改dom增加class
export function addClass(el, className){
if(hasClass(el, className)){
return;
}
let newClass=el.className.split(' ');
newClass.push(className);
el.className=newClass.join(' ');
}
9. 生成随机整数
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1) + min);
}
10. 打乱数组顺序, 洗牌算法
function shuffle(arr) {
let _arr = arr.slice(); //深拷贝数组
for (let i = 0; i<_arr.length; i++) {
let j = getRandomInt(0, i);
let t = _arr[i];
_arr[i] = _arr[j];
_arr[j] = t;
}
return _arr;
}
11. 判断是否为纯粹的对象,由 new Object 或者 {} 创建的
function isPlainObject ( obj ) {
var proto, Ctor;
var toString = Object.prototype.toString;
var getProto = Object.getPrototypeOf; // 不是挂在原型上的
var hasOwn = Object.prototype.hasOwnProperty;
var fnToString = hasOwn.toString;
var ObjectFunctionString = fnToString.call(Object);
if ( !obj || toString.call( obj ) !== "[object Object]" ) {
return false;
}
proto = getProto( obj );
// 没有原型对象的对象 (`Object.create( null )`)
if ( !proto ) {
return true;
}
// 通过 new Object 出来的对象
Ctor = hasOwn.call( proto, "constructor" ) && proto.constructor;
return typeof Ctor === "function" && fnToString.call( Ctor ) === ObjectFunctionString;
}
12. 移动端多次点击事件
var times = 0; // 记录点击次数
var timer;
const CLICK_TIME = 5; // 比如连续点击5此就触发
document.body.onclick = function () {
if (timer) {
clearTimeout(timer);
timer = null;
}
if (times >= CLICK_TIME) {
// do something
}
times++;
timer = setTimeout(function () {
times = 0;
}, 200)
}
---
const REWARD_TICKET = 100
function getRandomArray(count) {
let arr = [];
let remainTicket = 0;
for (let i = 1; i < count; i++) {
if (i === 1) {
remainTicket = REWARD_TICKET;
} else {
remainTicket = REWARD_TICKET - arr.reduce((prev, curr) => {
return prev + curr;
});
}
if (remainTicket < 11) {
arr.push(remainTicket);
break;
}
const randomNumber = parseInt(Math.random() * remainTicket, 10);
arr.push(randomNumber > 10 ? randomNumber : 10);
}
// make sure every number >= 10
if (arr[arr.length - 1] < 10) {
const cut = arr.splice(arr.length - 2, 2);
arr.push(cut[0] + cut[1]);
}
return arr;
}
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。