Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass a JavaScript function as parameter

How do I pass a function as a parameter without the function executing in the "parent" function or using eval()? (Since I've read that it's insecure.)

I have this:

addContact(entityId, refreshContactList()); 

It works, but the problem is that refreshContactList fires when the function is called, rather than when it's used in the function.

I could get around it using eval(), but it's not the best practice, according to what I've read. How can I pass a function as a parameter in JavaScript?

like image 816
imperium2335 Avatar asked Nov 08 '12 09:11

imperium2335


People also ask

Can you pass function as parameter in JavaScript?

Functions in the functional programming paradigm can be passed to other functions as parameters. These functions are called callbacks. Callback functions can be passed as arguments by directly passing the function's name and not involving them.

How do you pass a function as a parameter?

In JavaScript, passing a function as a parameter to another function is similar to passing values. The way to pass a function is to remove the parenthesis () of the function when you assign it as a parameter.

Can we pass function as parameter?

Because functions are objects we can pass them as arguments to other functions. Functions that can accept other functions as arguments are also called higher-order functions. In the example below, a function greet is created which takes a function as an argument.

Can you use a function as a parameter of another function?

Functions can be passed into other functionsFunctions, like any other object, can be passed as an argument to another function.


2 Answers

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); 
like image 112
Fenton Avatar answered Sep 22 '22 03:09

Fenton


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"]);  
like image 21
dallin Avatar answered Sep 25 '22 03:09

dallin