防抖
let btn = document.getElementsByClassName('btn')[0];
const debounce = function debounce(fn, interval, direction) {
let timer = null;
if (typeof fn !== 'function') throw new TypeError(`fn is not function!!!`);
if (typeof interval === 'boolean') direction = interval;
if (typeof direction !== 'boolean') direction = false;
if (typeof interval !== 'number') interval = 500;
return function execute(...args) {
let ishead = direction === true && timer === null;
timer = mycleartimer(timer);
timer = setTimeout(() => {
timer = mycleartimer(timer);
if (!direction) {
fn.call(this,...args);
};
}, interval)
if (ishead) fn();
}
}
const fn = function fn(e) {
console.log(e,this)
}
const mycleartimer = function mycleartimer(timer) {
if (typeof timer !== 'null') {
clearTimeout(timer);
return null
}
return timer
}
btn.addEventListener("click", debounce(fn, 500, false));
节流
let btn = document.getElementsByClassName('btn')[0];
const fn = function fn(){
console.log(11)
}
const clearTimer = function clearTimer(timer){
if(timer!=='null'){
clearTimeout(timer);
return null;
}
return timer;
}
const throttle = function throttle(fn,wait){
let timer = null,
previous = 0;
return function operate(){
let now = +new Date(),
remaining = wait - (now - previous);
if(remaining < 0){
timer = clearTimer(timer);
fn();
previous = +new Date();
}else if(timer === null){
timer = setTimeout(() => {
timer = clearTimer(timer);
previous = +new Date();
fn();
},remaining)
}
}
}
btn.addEventListener("click", throttle(fn, 1000));
检测是否是一个函数
const isFunction = function isFunction(obj){
return typeof obj ==='function'
}
console.log(isFunction(()=>{}));
检测是否是一个window对象
var isWindow = function isWindow(obj) {
return obj != null && obj === obj.window;
};
标准的检测数据类型的办法
let toType = function toType(obj){
let reg = /^\[object ([a-zA-Z0-9]+)\]$/i;
return reg.exec(({}).toString.call(obj))[1].toLowerCase()
}
console.log(toType(null));
console.log(toType(1));
console.log(toType({}));
console.log(toType(function (){}));
检测是否为数组或者类数组
const isArrayLike = function isArrayLike(obj){
if(obj == null) {
return false
}
return toType(obj) === "array" || toType(obj) === 'object' && obj.hasOwnProperty(length) && obj.hasOwnProperty(obj.length-1)
}
console.log(isArrayLike([]))
检测是否为纯粹的对象「直属类是Object || Object.create(null)」
var isPlainObject = function isPlainObject(obj){
if(toType(obj) !== 'object') return false
return !Object.getPrototypeOf(obj) || Object.getPrototypeOf(obj) === Object.prototype
}
检测是否是空对象
var isEmptyObject = function isEmptyObject(obj) {
var keys = Object.keys(obj);
if (typeof Symbol !== "undefined") keys = keys.concat(Object.getOwnPropertySymbols(obj));
return keys.length === 0;
};
console.log(isEmptyObject({name:'yzl',age:18,[Symbol()]:'111'}))
检测是否为数组
var isNumeric = function isNumeric(obj) {
return toType(obj) === 'number' && !isNaN(obj)
}
深浅拷贝
let merge = function merge() {
let deep = false,
target = arguments[0],
i = 1,
length = arguments.length;
if (typeof target === 'boolean') {
deep = target;
target = arguments[i] || {};
i++;
}
for (; i < length; i++) {
for (name in arguments[i]) {
let copy = arguments[i][name];
if(/^(object||function)$/i.test(toType(copy)) && deep === true){
target[name] = merge(true,target[name],copy);
}else if(copy != null){
target[name] = copy
}
}
}
return target;
}
深浅克隆
const clone = function clone() {
let target = arguments[0],
deep = false,
length = arguments.length,
result = {};
if (typeof target === 'boolean') {
if (length == 1) return target;
deep = true;
target = arguments[1];
}
if (target == null) return target;
if (/^(regexp|date|error)$/i.test(toType(target))) {
if (toType(target) === 'error') target = target.message;
let ctor = target.constructor;
return new ctor(target);
}
for (key in target) {
let copy = target[key];
if (deep && toType(copy) === 'object' || toType(copy) === 'array' || toType(copy) === 'function') {
result[key] = clone(deep, copy);
} else {
result[key] = copy
}
}
return result;
}
let obj = {
name: 'yzl',
age: [1,2,3,{
name:'sjl'
}],
arr:[1]
}
let obj2 = clone(obj);
obj.age[3].name = 'lll';
console.log(obj.age[3].name,obj2.age[3].name)
console.log(obj.arr === obj2.arr)
console.log(obj.fn === obj2.fn)
数组去重
let arr = [1,5,7,3,2,3,2];
~function () {
const noRepetition = function noRepetition() {
for (let i = 0; i < this.length; i++) {
for (let j = i+1; j < this.length; j++) {
if(this[i] === this[j]){
this.splice(j,1)
}
}
}
return this
};
['noRepetition'].forEach((v, i) => {
Array.prototype[v] = eval(v);
})
}()
console.log(arr.noRepetition())
let arr = [1,5,7,3,2,3,2];
~function () {
const noRepetition = function noRepetition() {
let obj = {},newarr = [];
for (const v of this) {
obj[v] = 'd'
}
for (const key in obj) {
newarr.push(key);
}
return newarr
};
['noRepetition'].forEach((v, i) => {
Array.prototype[v] = eval(v);
})
}()
console.log(arr.noRepetition())