Getting a better understanding of callback functions in JavaScript -


I have to understand another function in the function as a callback and execute it, but I understand the best implementation Not to do this. I am looking for a very basic example, such as:

  var myClallExample = {myFirstFunction: function (param1, param2, callback) {// something with param1 and param2 if ( Arguments.length == 3) {// execute the callback function. // What is the best way to do this? }}, MySecondFunction: function () {myFirstFunction (wrong, true, function () {// execute it when called this anonymous function}}); }};  

In myFirstFunction, if I return a new callback (), then it works and executes the anonymous function, but it does not look like the right approach for me.

You can just say

  callback ();  

Alternatively you can use the call method if you want to adjust the value of this within the callback. .

callback.call (newValueForThis);

inside the function will be this will be newValueForThis .


Comments