Explains javascript function and callback required

im learning javascript and I am following some youtube video tutorial

this is the source code

function add(first, second, callback){ console.log(first+second); callback(); } function logDone(){ console.log("done"); } add(2,3,logDone); 

and the result of the code above is

5 main.js (line 4)

done by main.js (line 9)

and I make a slight code change in this

 function add(first, second, callback){ console.log(first+second); callback; } function logDone(){ console.log("done"); } add(2,3,logDone()); 

and the result of this is

done by main.js (line 9)

5 main.js (line 4)

my question is:

  • Could you explain to me why I got the result?

  • and what's the difference if we call a function with a bracket () and without a bracket ()?

+7
javascript
source share
5 answers

Explanation of the first fragment

 function add(first, second, callback) { console.log(first + second); callback(); // run whatever the "callback" function is } function logDone() { console.log("done"); } add(2, 3, logDone); // pass in a function (not an invocation of a function) the // function isn't run here 

Explanation of the second fragment

 function add(first, second, callback) { console.log(first + second); callback; // display the value of whatever "callback" is } function logDone() { console.log("done"); } add(2, 3, logDone()); // run "logDone" and then pass the result (which in this // case is undefined) into add 

As you can see, the first fragment of the code does not actually start the callback until in the add function, while the second fragment starts the callback to add so that it can pass everything that comes back from logDone to add .

+8
source share

Perhaps you will become more clear when I change the logDone to this:

 var logDone = function() { console.log("done"); } 

The logDone identifier is basically just a variable that refers to a function. To execute (also: call or: call) the function that you add parentheses: logDone() .

So, in the first example, you simply pass the function as the third argument to add() , which is then executed inside add() , with callback(); .

In your second example, however, you execute the function with logDone() immediately, which causes the return value of logDone() 1 to be passed as the third argument to the add() call. In other words, the first logDone (the first log message appears as a result), then add is executed (the second log message appears as a result).

In addition, a callback; statement callback; , inside add , does nothing. And if you would leave parentheses as in the first example, this would lead to an error, because undefined 2 is not a function.


1), which in this case is undefined , because logDone() does not explicitly return anything.

2) the value that was the result of calling logDone() .

+4
source share

and what's the difference if we call a function with a bracket () and without a bracket ()?

If you have () , you call it. If you do not, then you do not.

Could you explain to me why I got the result?

In the first example, you pass the add function, and then the add function calls it.

In the second example, you call the function and pass its return value ( undefined ) to add , which then mentions it in the statement, but does nothing with it.

+3
source share

When you execute func([args=optional]) , func is called or called.

and what's the difference if we call a function with a bracket () and without a bracket ()?

We call the function only when we do () (arguments are optional). When a function does not have parentheses, you simply use its link.

By storing the link, execute the function in a variable, you can call it later, which makes callback .

In the second snippet, since nothing is returned, the callback will be undefined . Try calling it by executing callback() in the second snippet and you will see an error message,

undefined not a function.

+3
source share

In the first example, you pass the function as a parameter.
In the second example, you pass the result of the function after the call as a parameter.

+3
source share

All Articles