手写一个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 @ 2022-04-02 15:12  musicBird  阅读(36)  评论(0)    收藏  举报