Morteza Sepehri Niya Morteza Sepehri Niya - 4 years ago 92
Javascript Question

What does function in function do exactly in nodejs?

See this example:

var tools1 = require('../tools/tools1');
var test_func = function(arg1, arg2, arg3) {

var local_var_1 = "lc1";
var local_var_2 = "lc2";

return function(data) {
var result = tools1.doSth(local_var_1);
result = result+local_var_2;

exports.test_func = test_func;

I do not understand what does inner function do what it is for!

Answer Source

In javascript when you return function it returns reference of that function and you can call it later.

Like when you do var result = test_func(), result will hold reference of that function. Then later you can call that returned function like result(data).

A basic example:

function sum(x, y) {
  var rs = x+y;
  return function(message) {
    console.log(message + rs); //rs holds its value because of clousers

var result = sum(2, 3);
result("This is result: ");

Variables that are used locally, but defined in an enclosing scope like rs in my example because of Closures

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download