Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When should I store a function into a variable?

I'm learning JavaScript at the moment and I don't quite understand when to write a function into a variable.

For instance, both of the following code blocks do the exact same thing in Node.js:

 var onReq = function(req, res) {
   res.write('Hello');
 };

 http.createServer(onReq).listen(3000);

and

function onReq(req, res) {
   res.write('Hello');
 }

 http.createServer(onReq).listen(3000);



Which is the best method to do according to best practices, and why?

like image 298
imjp Avatar asked Mar 08 '12 13:03

imjp


People also ask

Can you store a function in a variable?

Functions stored in variables do not need function names. They are always invoked (called) using the variable name. The function above ends with a semicolon because it is a part of an executable statement.

How do you put a function into a variable?

we put the function in a variable if inside the function block we use the return method: var multiplyTwo = function (a) { return a * 2; };

What can I store in a variable?

Many variables store numbers and strings, like the ones above. Variables can also store other types of data, like lists, dictionaries, and Boolean values (true/false).

Can you store a function in a variable python?

In Python, we can assign a function to a variable. And using that variable we can call the function as many as times we want. Thereby, increasing code reusability. Simply assign a function to the desired variable but without () i.e. just with the name of the function.


3 Answers

Usually I'll only use a var funcName = function(){} when I would need to redefine the action(s) for that function later on. For example:

var foo = function(a){ return a * 2; }
var bar = foo(2);

foo = function(a){ return a / 2; }

bar = foo(bar);

Otherwise, for most purposes (assuming it's not a callback or a modifier) declaring a function "classically" is usually acceptable.

like image 70
Brad Christie Avatar answered Oct 19 '22 07:10

Brad Christie


I default to the non-variable function onReq(){} version. It's not a concious decision I've made, but thinking about it brings forth these arguments:

  • It looks cleaner.
  • It is conceptually simpler: it's just a function, while the other is a function and a variable. It's a small thing, but I find it valuable none the less.
  • It assures me that onReq will always refer to that function body - one less thing to consider when reading the code. Sort of like marking a variable as final in Java.
  • Keeps me from "accidentally" replacing the function, causing unintended side effects elsewhere.
like image 39
Supr Avatar answered Oct 19 '22 08:10

Supr


Here is an explaination:

There is a distinction between the function name and the variable the function is assigned to:

  • The function name cannot be changed, while the variable the function is assigned to can be reassigned.
  • The function name can be used only within the function's body. Attempting to use it outside the function's body results in an error (or undefined if the function name was previously declared via a var statement).

https://developer.mozilla.org/en/JavaScript/Reference/Functions_and_function_scope

like image 3
Churk Avatar answered Oct 19 '22 08:10

Churk