You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Function.prototype.bind2=function(context, ...args){if(typeofthis!=='function'){thrownewTypeError('Bind must be called on a function')}constexecuteBound=function(sourceFunc,boundFunc,context,callingContext,args){if(!(callingContextinstanceofboundFunc)){returnsourceFunc.apply(context,args)}else{constself=Object.create(sourceFunc.prototype)constresult=sourceFunc.apply(self,args)if(result&&typeofresult==='object'||typeofresult==='function'){returnresult}else{returnself}}}constfunc=thisconstbound=function(...innerArgs){returnexecuteBound(func,bound,context,this,args.concat(innerArgs))}returnbound}constshowName=function(sex,age){console.log(this,sex,age)}showName.bind2({name: '前端胖头鱼'},'boy')(100)constPerson=function(name){this.name=name}Person.prototype.showName=function(age){console.log(this,this.name,age)}constbindPerson=Person.bind(null,'boy')constp1=newbindPerson('前端胖头鱼')p1.showName(100)
本文已参与 「掘力星计划」 ,赢取创作大礼包,挑战创作激励金。
前言
前几天写了一篇 【中高级前端】必备,30 + 高频手写实现及详细答案 (万字长文),你值得拥有噢总结了 30 + 常见手写实现,广大兄弟姐妹指出了其中不少问题,还有人提出没有防抖和节流等实现,胖头鱼不吃不睡又搞了 12 + 手写题(已接近 42+),一起来看看吧。
直通车
点击查看日拱一题源码地址(目前已有 62 + 个手写题实现)
1. 防抖
2. 节流
基于时间戳 (方式 1)
基于 setTimeout(方式 2)
3. 函数柯里化
4. bind
姐妹篇 call 实现
姐妹篇 apply 实现
5. 实现一个简易版模板引擎
6. 类数组转化为数组的 4 种方式
7. 请实现 DOM2JSON 一个函数,可以把一个 DOM 节点输出 JSON 的格式
测试
8. 列表转树形结构
9. 树形结构转列表
10. sleep
11. 菲波那切数列
暴力实现
缓存优化
这个过程中如果按照上面暴力实现的代码会重复多次计算某些曾经计算过的值,比如 8、7、6、5... 等等,这个损耗是没有必要的,所以我们可以把计算的结果进行缓存,下次遇到求同样的值,直接返回即可
12. 实现一个函数 sum 函数
分析
仔细观察这几种调用方式可以得到以下信息
看起来是不是有点
函数柯里化
的感觉,前面的函数调用仅仅是在缓存每次调用的参数,而 valueOf 的调用则是拿着这些参数进行一次求和运算并返回结果结尾
The text was updated successfully, but these errors were encountered: