K
K
KnightForce2018-03-14 23:20:43
JavaScript
KnightForce, 2018-03-14 23:20:43

How do Function.bind.apply and function.apply.bind work?

I have given examples of such functions Function.bind.apply() < and /b>function.apply.bind();
Why and what do they do?
What is the point in them, especially when referring to Function?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Y
youngmysteriouslight, 2018-03-15
@youngmysteriouslight

I assume that the description and principle of operation of bind and apply are known to us.
For simplicity of reasoning, it is convenient to consider that this is, as it were, a zero argument.
We need the following: g.bind(t, x)conditionally equivalent

function (a) {
  return g.call(t, x, a);
}

Strictly speaking, Function.applyit is not the same as Function.prototype.apply. But in this case, as we will see, they are interchangeable because their meanings are the same (point to the same function with the same code). returns a function, roughly speaking (because it is implicitly assumed that this function will be called in the future with exactly one argument), of this form
function (a) {
  return Function.apply.call(f, null, a);
}
which is conditional (because the change occurs here fas an object; actually called applywith this=f) is equivalent to
function (a) {
  f.apply = Function.apply;
  return f.apply(null, a);
}
Since the function f under normal circumstances inherits the same apply as Function, is completely equivalent to . Why is it important to specify null Because if you do not specify it, you get a functionFunction.apply.apply(f, null, a)f.apply(null, a)
function (a) {
  return f.apply(a);
}
i.e. awill be passed as this.
This might make sense too. Compare:
alternative example
function foo(bar,baz) {
  var x = bar * baz;

  return [
    Promise.resolve(x),
    Promise.resolve(x*2 )
  ];
}

Promise.all(
  foo( 10, 20 )
)
.then( Function.apply.bind(
  function() {
    console.log( this[0], this[1] );
  }
) );

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question