Alexander Mills
Alexander Mills

Reputation: 100200

Function.prototype.bind with arguments object

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.

Upvotes: 1

Views: 47

Answers (3)

Shishir Arora
Shishir Arora

Reputation: 5933

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

Upvotes: 2

Alexander Mills
Alexander Mills

Reputation: 100200

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));

Upvotes: -1

SamVK
SamVK

Reputation: 3425

.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])

Upvotes: 2

Related Questions