M
M
Max Rudik2015-11-03 21:00:52
JavaScript
Max Rudik, 2015-11-03 21:00:52

How to properly call a callback function?

Good evening everyone!
I have a theoretical question, I can not understand the syntax and logic of using callback functions. Everything that I googled - talks about the same examples and does not give me an understanding of the following points:

var func = function(callback){
    var name = "Max";
    callback(name);
  };
func(function(n){
    console.log("Hi " + n);
  });

1) An anonymous function is passed to the funk variable with an argument in the form of a callback function. So? Farther.
In the main function: 1) the name variable is declared 2) the callback function is called with the name argument...
But it hasn't been described yet, has it? Or is it not a function call, but something else? What?
2) Call func(); with a callback in the form of an anonymous function with an argument n. When printed to the console, it turns out that n got the value of the name variable from the func function declaration. What does this follow from? How are n and name related?
Thanks in advance for your replies. The question is pretty confusing, but that's the question, not the answer.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
Y
Yaroslav Lyzlov, 2015-11-03
@dixoNich

Functions Have Parameters
When you call a callback function, it's the same as calling fn with parameters like this.
Your case

function fn (a, b, c) { alert(a, b, c) }

function foo (callback) { 
   var a = 1, b = 2, c = 3
   callback(1, 2, 3) // равносильно fn (1, 2, 3)
}

foo(fn)

It is not necessary to pass an anonymous function, any function.
Summary: You would first deal with the functions.
Read this, for example: https://learn.javascript.ru/function-basics

I
Ivanq, 2015-11-03
@Ivanq

Hm... A function can be written into a variable, into anything. Imagine an example:

function a(name) {
    alert(name);
}
a("Vanya");

Look - the string has not yet been created, but it is already used in the function! Same with callback:
function a(name, callback) {
    alert(name);
    callback(name);
}
a("Vanya", function(name) { console.log(name); });

Look, it's almost the same here. Perhaps you are confused by the callback function ? Then here's the answer - everything in js is represented by variables and properties, even functions. Thus, the function in the callback variable is called ! And callback is a variable that contains an anonymous function.
Also, as Yaroslav Lyzlov already noted , you can pass not only an anonymous function.

M
Max Rudik, 2015-11-03
@mvr1976

Here, creatively rethought, everything seems to be clear and everything works:

function fuck(girl, boy, callback){
  var hookUp;
  if(girl >=18 && boy >=18){
    hookUp = true;
  }else hookUp = false;	
  callback(hookUp);
}
fuck(19,30, function(hookUp){
  if(hookUp){
    console.log("You're welcome!");	
  }else console.log("No way!!!");
});

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question