相关学习推荐:javascript
1. 防抖
function debounce(func, ms = 500) { let timer; return function (...args) { if (timer) { cleartimeout(timer); } timer = settimeout(() => { func.apply(this, args); }, ms); };}复制代码2. 节流
function throttle(func, ms) { let canrun = true; return function (...args) { if (!canrun) return; canrun = false; settimeout(() => { func.apply(this, args); canrun = true; }, ms); };}复制代码3. new
function mynew(func) { const instance = {}; if (func.prototype) { object.setprototypeof(instance, func.prototype); } const res = func.apply(instance, [].slice.call(arguments, 1)); if (typeof res === "function" || (typeof res === "object" && res !== null)) { return res; } return instance;}复制代码4. bind
function.prototype.mybind = function (context = globalthis) { const fn = this; const args = array.from(arguments).slice(1); const newfunc = function () { if (this instanceof newfunc) { // 通过 new 调用,绑定 this 为实例对象 fn.apply(this, args); } else { // 通过普通函数形式调用,绑定 context fn.apply(context, args); } }; // 支持 new 调用方式 newfunc.prototype = fn.prototype; return newfunc;};复制代码5. call
function.prototype.mycall = function (context = globalthis) { // 关键步骤,在 context 上调用方法,触发 this 绑定为 context context.fn = this; let args = [].slice.call(arguments, 1); let res = context.fn(...args); delete context.fn; return res;};复制代码6. apply
function.prototype.myapply = function (context = globalthis) { // 关键步骤,在 context 上调用方法,触发 this 绑定为 context context.fn = this; let res; if (arguments[1]) { res = context.fn(...arguments[1]); } else { res = context.fn(); } delete context.fn; return res;};复制代码7. deepcopy
function deepcopy(obj, cache = new weakmap()) { if (!obj instanceof object) return obj; // 防止循环引用 if (cache.get(obj)) return cache.get(obj); // 支持函数 if (obj instanceof function) { return function () { obj.apply(this, arguments); }; } // 支持日期 if (obj instanceof date) return new date(obj); // 支持正则对象 if (obj instanceof regexp) return new regexp(obj.source, obj.flags); // 还可以增加其他对象,比如:map, set等,根据情况判断增加即可,面试点到为止就可以了 // 数组是 key 为数字素银的特殊对象 const res = array.isarray(obj) ? [] : {}; // 缓存 copy 的对象,用于出来循环引用的情况 cache.set(obj, res); object.keys(obj).foreach((key) => { if (obj[key] instanceof object) { res[key] = deepcopy(obj[key], cache); } else { res[key] = obj[key]; } }); return res;}复制代码8. 事件总线 | 发布订阅模式
class eventemitter { constructor() { this.cache = {}; } on(name, fn) { if (this.cache[name]) { this.cache[name].push(fn); } else { this.cache[name] = [fn]; } } off(name, fn) { const tasks = this.cache[name]; if (tasks) { const index = tasks.findindex((f) => f === fn || f.callback === fn); if (index >= 0) { tasks.splice(index, 1); } } } emit(name) { if (this.cache[name]) { for (let fn of this.cache[name]) { fn(); } } } emit(name, once = false) { if (this.cache[name]) { // 创建事件副本,如果回调函数内继续注册相同事件,触发时,会造成死循环 const tasks = this.cache[name].slice() for (let fn of tasks) { fn(); } if (once) { delete this.cache[name] } } }}复制代码9. 柯里化:只传递给函数一部分参数来调用它,让它返回一个函数去处理剩下的参数
function curry(func) { return function curried(...args) { if (args.length >= func.length) { func.apply(this, args); } else { return function (...args2) { curried.apply(this, args.concat(args2)); }; } };}复制代码10. es5 实现继承
function create(proto) { function f() {} f.prototype = proto; return new f();}// parentfunction parent(name) { this.name = name;}parent.prototype.say = function () { console.log(this.name);};// childfunction child(age, name) { parent.call(this, name); this.age = age;}child.prototype = create(parent.prototype);child.prototype.constructor = child;child.prototype.say = function () { console.log(th
嘉兴网站优化公司如何通过微信群做好网络营销如何申请注册域名 域名收费标准是怎样的营销型网站建设的重点之用户体验什么时候可以app域名注册?什么是app域名?企业网站改版费用是多少?有参考标准?域名对我们有什么作用?我们怎么买域名呢?怎么免费注册域名网,要注意哪些问题关于如何注册网站中遇到的一些问题?