Javascript is applied and methods are invoked and combined together

In this article , the js log function contains an instruction:
Function.prototype.apply.call (console.log, console, arguments);

I am really confused by this statement.

  • What is he doing?
  • How can I analyze this statement?
  • Or with some thoughts or tools, can I understand this step by step?
  • Can more statements be simplified to achieve the same result? eg:var temp = Function.prototype.call(console.log, console, arguments); Function.prototype.apply(temp);

Thanks for the answer.

+2
source share
3 answers

Apply is a function on a function prototype. The call is also a function of the prototype function. Apply is a function, so she calls it a prototype. All this makes a call to the apply function.

: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/apply

+1

.

Function.prototype.apply, myBigFatFunction.apply, this , . apply call , , . :

myStr.substring(5)
String.prototype.substring.apply(myStr, [5]);
String.prototype.substring.call(myStr, 5);
^ all equivalent

, , , , JavaScript, (, console.log.apply). , ; console this, log , .

, arguments. , , , ( Function.prototype.apply)

console.log console , arguments , . , , Function.prototype.bind, .

+1

?

console.log console this , pseudo-array arguments . , arguments "hi", "there" , , :

console.log("hi", "there");

?

, ?

apply call: this . apply ( ). call .

apply call, :

someFunction.apply(valueForThis, ["arg1", "arg2", "arg3"]);
// or
someFunction.call(valueForThis, "arg1", "arg2", "arg3");

apply call , (apply= in - , call= ).

, ?

console.log.apply(console, arguments);

: console.log - , . , JavaScript apply. , console.log, apply.

Function.prototype. , JavaScript. - , apply call JavaScript. , , console.log (, Function.prototype), apply .

, call apply apply console.log.

Can more applications be simplified to achieve the same result?

Not very, not so much that we can share. I will try to use variable names to clarify:

var thisValue = console;
var functionToCall = console.log;
var applyFunction = Function.prototype.apply;
applyFunction.call(functionToCall, thisValue, arguments);
+1
source

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


All Articles