0
votes

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);
        };
    },
1
This looks wrong to me 'cos it doesn't let the user bind persistant args and enter their own args at the same timePaul S.
I know what bind does, but there is so much extra code hereairnet
@airnet it's looks like it's supposed to be a shimPaul S.

1 Answers

2
votes

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