The Corialization problem of realizing bind function

A quick implementation is generally as follows:

Function.prototype.bind = function (context) {
    var me = this;
    var argsArray = Array.prototype.slice.call(arguments);
    return function () {
        return me.apply(context, argsArray.slice(1))
    }
}

but recently I read an article, saying that this will cause
clipboard.png
function loss of preset parameters. What does it mean?

Function.prototype.bind = Function.prototype.bind || function (context) {
    var me = this;
    var args = Array.prototype.slice.call(arguments, 1);
    return function () {
        var innerArgs = Array.prototype.slice.call(arguments);
        var finalArgs = args.concat(innerArgs);
        return me.apply(context, finalArgs);
    }
}

Why do you want to create an innerArgs, Corialization?

Please give a specific scene explanation. Thank you

Feb.27,2021

the preset parameter is the parameter specified before you start binding, so you don't need to pass it any more.
when innerArgs is called later, the parameter passed


your first call to the new function argument is useless

function test(a){
    console.log(a);
}

var newTest = test.bind();
newTest(1);

    function a () {console.log(arguments)}
    // bind
    var b = a.bind(null, 1, 2)
    b(3,4)

Native bind enables a function to have preset initial parameters, that is, to implement partial function

function sum(a, b) {
    console.log(a + b)
}

var sum2 = sum.bind(null,2);   // a 2
sum2(4)                        // b 4 6

but the first bind method you imitate does not have this function, and passing parameters to the function returned by bind (sum2) has no effect. The second bind method of
implements this function of preset initial parameters. The sum2 function passes parameter 4, which is collected and merged with argument 2, which was initially passed using the bind method, and then passed to the sum function. The effect of sum (2,4) is realized.

MDN there is bind about the application of partial functions. You can take a look at it again.

Please point out where you speak poorly.

Menu