手写一个bind

复制代码
 1 Function.prototype.bind1 = function(){
 2             // 将类数组转化成数组
 3             let arr = Array.prototype.slice.call(arguments)
 4             
 5             //提取this
 6             const t = arr.shift()
 7 
 8             //保存当前this
 9             const self = this
10             
11             return function(){
12                 return self.apply( t, arr )
13             }
14 }
15 
16 function fn1(a,b,c){
17     console.log(this)
18     console.log(a,b,c)
19     return 'this is fn1'
20 }
21 const fn2 =  fn1.bind1({x:100},1,2,3)
22 console.log(fn2())
复制代码

 

posted @   musicBird  阅读(29)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 25岁的心里话
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
点击右上角即可分享
微信分享提示