Skip to content Skip to sidebar Skip to footer

How Exactly Callbacks Get Their Arguments?

I have trouble understanding how callbacks in JavaScript get their arguments. Or in other words: how to implement a higher-order function so its callback accepts for instance a sta

Solution 1:

Arguments are passed to a function when that function is called.

functionfoo(arg) {
    console.log(arg);
}

foo("This is the value");

This is still true when it is a callback function.

functionfoo(arg) {
    console.log(arg);
}

functionbar(callback) {
    callback("This is the value");
}

bar(foo);

And it is still true when the callback function is called by code written by someone else that exists in a library you aren't examining the source code of.

Post a Comment for "How Exactly Callbacks Get Their Arguments?"