What does "a.call.apply(a.bind, arguments)" mean?



Just because of curiosity I wanted to make a bounded function with this particular approach :

var fn = function(a, b, c) {
    return a.call.apply(a.bind, arguments)

var boundFn = function(a, b, c) {
    fn.apply(null, arguments)

function unboundedFn() {
    console.log(this, arguments)

var boundedFn = boundFn(unboundedFn, x, y);

So I’m trying to understand what a.call.apply(a.bind, arguments) do exactly ?


If You have a function like this:

function unboundedFn() {
    console.log(this, arguments)

You can use unboundedFn.call(thisObj, arg1, arg2) or unboundedFn.apply(thisObj, [arg1, arg2]) to run it, but changing what this means inside. Both call and apply do the same, and only difference is way of passing arguments.

Becouse call, apply and also bind are methods, you can run for example unboundedFn.call.call.call.call.apply.apply.apply.apply(), but it doesn’t seem to have to much sense.

In your example return a.call.apply(a.bind, arguments) is equal to return a.bind.call(...arguments), which is equal to a.bind(...arguments.slice(1)), so whole fn function can be simplified to:

function fn(a,b,...args){
   return a.bind(b, ...args);

Answered By – Mateusz Krawczyk

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave A Reply

Your email address will not be published.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More