1

say I have this simple situation:

const f = function(){

 const fn = f.bind(null, arguments);

}

I am trying to implement a "retry" mechanism - this code checks out according to the static analysis and seems to the signature of Function.prototype.bind, but my question is:

is arguments going to be applied as simply the first argument to f or will it be spread out, as in, f.apply(null, arguments)? I am having trouble finding an example of this online.

5
  • 4
    const fn = f.bind(null, ...arguments); to spread Commented Nov 8, 2017 at 2:06
  • 1
    Use f.apply.bind(f, null, arguments) or f.bind.apply(f, [null, ...arguments]) Commented Nov 8, 2017 at 2:09
  • Since you're on node.js, use ES6! function f(...args) { const fn = () => f(...args); } Commented Nov 8, 2017 at 2:10
  • yeah I have seen the spread operator work with arguments, but for some reason I had to do ...Array.from(arguments)...not sure why. Commented Nov 8, 2017 at 3:09
  • @Bergi the example in question is a way oversimplified situation, as it should be...want to see the real thing? :) github.com/sumanjs/suman/blob/feature_alexamil_1509956540678/… Commented Nov 8, 2017 at 3:23

3 Answers 3

2

.bind works similar to .call, not .apply - the second argument will be treated as just that, the second argument. So in your example:

f(1,2,3) would produce fn([1,2,3])

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

Comments

2

arguments will be passed as simply the first argument to f. Moreover, if you then call the bound function with more arguments, those arguments will come after the bound one (i.e. you cannot overwrite that first argument).

read more here

1 Comment

Link doesn't seem ok.
-1

Yeah, so Function.prototype.bind has similar signature to Function.prototype.call

so you need to do:

fn.bind(null, ...arguments);

or

fn.bind(null, ...Array.from(arguments));

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.