You just need to remove the parenthesis:
addContact(entityId, refreshContactList);
This then passes the function without executing it first.
Here is an example:
function addContact(id, refreshCallback) {
refreshCallback();
// You can also pass arguments if you need to
// refreshCallback(id);
}
function refreshContactList() {
alert('Hello World');
}
addContact(1, refreshContactList);
You can also pass anonymous functions:
function addContact(id, refreshCallback) {
refreshCallback();
// You can also pass arguments if you need to
// refreshCallback(id);
}
addContact(1, () => alert('hello world'));
Answer from Fenton on Stack OverflowYou just need to remove the parenthesis:
addContact(entityId, refreshContactList);
This then passes the function without executing it first.
Here is an example:
function addContact(id, refreshCallback) {
refreshCallback();
// You can also pass arguments if you need to
// refreshCallback(id);
}
function refreshContactList() {
alert('Hello World');
}
addContact(1, refreshContactList);
You can also pass anonymous functions:
function addContact(id, refreshCallback) {
refreshCallback();
// You can also pass arguments if you need to
// refreshCallback(id);
}
addContact(1, () => alert('hello world'));
If you want to pass a function, just reference it by name without the parentheses:
function foo(x) {
alert(x);
}
function bar(func) {
func("Hello World!");
}
//alerts "Hello World!"
bar(foo);
But sometimes you might want to pass a function with arguments included, but not have it called until the callback is invoked. To do this, when calling it, just wrap it in an anonymous function, like this:
function foo(x) {
alert(x);
}
function bar(func) {
func();
}
//alerts "Hello World!" (from within bar AFTER being passed)
bar(function(){ foo("Hello World!") });
If you prefer, you could also use the apply function and have a third parameter that is an array of the arguments, like such:
function eat(food1, food2) {
alert("I like to eat " + food1 + " and " + food2 );
}
function myFunc(callback, args) {
//do stuff
//...
//execute callback when finished
callback.apply(this, args);
}
//alerts "I like to eat pickles and peanut butter"
myFunc(eat, ["pickles", "peanut butter"]);
Basic JavaScript - Passing Values to Functions with Arguments - c49z7uqSwAiXrPuLhIIeT
Can someone explain how to pass a function as an argument to another function in JS and show me a code example? I only know OOP and find it really confusing to do this.
Why is passing a function as a parameter a necessity in callback functions instead of just simply executing inside a function?
Pass Class as parameter that extends another class
If you want a class type (and not an instance of the class), you can use:
const foo = <T: {new(): Bar}>( baz: T )
This will accept derived classes too, as their constructors also produce something that can cast to Bar.
More on reddit.comVideos
Can someone explain how passing a function as a parameter to another function works and what the syntax for it is? It's really confusing for a beginner in JS and someone who only knows OOPS.
Can you write a javascript code example where they pass a function as a parameter to another function and explain what's going on the syntax for it? Can't find any good / well-explained resources on it that are easy to understand.