Javascript calling and applying methods

I am confused by this code:

Function.prototype.apply.call(Math.floor, undefined, [1.75]); // 1

I read this javascript post applying and calling methods and chains together , and I understand that the value of the chain is applied and calling methods together.

However, I'm still confused by the syntax. The correct syntax to call is function.call(thisArg, arg1, arg2, ...). But in this case why the method call may take these parameters three ( target, thisArgument, argumentsList), which coincides with Reflect.apply(target, thisArgument, argumentsList)?

+4
source share
3 answers

, , , . , (target, thisArgument, argumentsList) apply, , apply , .

, :

Math.floor.apply(undefined, [1.75]);

Math.floor(1.75) // Obv with undefined as reference of this

+3
    Function.prototype.apply.call(Math.max, undefined, [1,75]); // 1

thisArgu Function.prototype.apply Math.max

thisArgu Function.prototype.apply Math.max

noramlly, Function.prototype.apply Function.prototype

thisArgu Function.prototype.apply Math.max :

Function.prototype Math.max, 'this' Function.prototype.apply()

'this':

    Math.max.apply(undefined,[1,75])

?

,

    Math.max.apply(undefined,[1,75])

:

    Window.max(1,75)//error;

, , 'this' Math.max, ;

+1

The syntax for call():

someFunctionOrMethod.call(...)

So this code calls the applytype method Function.

// Call the "apply()" method
Function.prototype.apply.call()

// With the following arguments passed to "apply"
(Math.floor, undefined, [1.75])
0
source

Source: https://habr.com/ru/post/1693339/


All Articles