【编程】八股文算法题大赏
防抖节流
// 💊点 每次都返回 一个函数
function Denounce(fn, wait = 500) {
let timer = null;
return function () {
// 每次都clearTime
if (timer) clearTimeout(timer);
timer = setTimeout(() => {
timer = null;
fn.apply(this, arguments);
}, wait);
};
}
function Throtte(fn, wait = 500) {
let timer = null;
return function () {
// 存在就拉闸 不然执行后续的
if (timer) return;
timer = setTimeout(() => {
timer = null;
fn.apply(this, arguments);
}, wait);
};
}
call bind apply
Function.prototype.myApply = function (ctx = globalThis) {
ctx.fn = this;
const args = [...arguments].slice(1);
const res = ctx.fn(args);
delete ctx.fn;
return res;
};
Function.prototype.myCall = function (ctx = globalThis) {
// 如果怕冲突 用 const fn = Symbol(ctx)
ctx.fn = this;
const args = [...arguments].slice(1);
const res = ctx.fn(...args);
delete ctx.fn;
return res;
};
Function.prototype.myBind = function (ctx = globalThis) {
const that = this;
const args = [...arguments].slice(1);
return function () {
return that.apply(ctx, [...arguments, ...args]);
};
};
赠人玫瑰, 手有余香。🌹
打赏
特别鸣谢
感谢以下用户对本文的支持与鼓励
加载打赏用户中
发表评论
文章评论
暂无任何评论,快去发表吧~