当前位置:网站首页>Handwritten code bind

Handwritten code bind

2022-06-10 20:15:00 Xiaoli Technology

function f1(a,b,c) {
    
    console.log('this', this);
    console.log(a,b,c);
    return 'this is fn1'
    
}

const fn = f1.bind({
    x:100}, 100,200,400)

Function.prototype.mybind = function () {
    
    //  Class arrays cannot use array methods directly , Array.prototype.slice.call  Convert to array form 
    //  Array of classes ===>  Convert to array 
    const args = Array.prototype.slice.call(arguments)

    // args = [1,2,3]

    //  obtain this
    const t = args.shift() //  Remove the first parameter ,  It's going to change the array ==>1,  The remaining  [2,3 ]

    const self = this

    return function () {
    
        return self.apply(t, args)
    }


}
原网站

版权声明
本文为[Xiaoli Technology]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/161/202206101916109044.html