0

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);
        };
    },
4
  • See developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… Commented Jan 6, 2015 at 15:15
  • 1
    This looks wrong to me 'cos it doesn't let the user bind persistant args and enter their own args at the same time Commented Jan 6, 2015 at 15:15
  • I know what bind does, but there is so much extra code here Commented Jan 6, 2015 at 15:15
  • @airnet it's looks like it's supposed to be a shim Commented Jan 6, 2015 at 15:16

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.