Answer the question
In order to leave comments, you need to log in
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
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);
}
Function.apply
it 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 formfunction (a) {
return Function.apply.call(f, null, a);
}
which is conditional (because the change occurs here f
as an object; actually called apply
with this=f
) is equivalent tofunction (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. a
will be passed as this
. 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 questionAsk a Question
731 491 924 answers to any question