airnet
airnet

Reputation: 2673

what the reasoning of arguments in return statement of bind function

I took this code from Leaflet api.

no idea what args and arguments is doing in return fn.apply(obj, args || arguments);

bind: function (fn, obj) { // (Function, Object) -> Function
        var args = arguments.length > 2 ? Array.prototype.slice.call(arguments, 2) : null;
        return function () {
            return fn.apply(obj, args || arguments);
        };
    },

Upvotes: 0

Views: 100

Answers (1)

Quentin
Quentin

Reputation: 943992

If you call bind with more than 2 arguments, then it will store those arguments. When you call the function returned by bind, then it will call it using those arguments.

If you call bind with 2 or fewer arguments, then when you call the function returned by bind, it will call it with the arguments you pass at the time.


This does not appear to be a correct implementation of bind. If you want a bind polyfill, then use the one from MDN

Upvotes: 2

Related Questions