Javascript nested function not defined

Tags:



Creating a nested function, and then attempting to fill in all function parameters results in an error:

function A(x) {
  function B(y) {
    function C(z) {
      console.log(x + y + z);
    }
  }
}

A(2)(2)(2);



>> Uncaught TypeError: A(...) is not a function

However on the MDN documentation, a nested function such as the following works correctly:

function outside(x) {
  function inside(y) {
    return x + y;
  }
  return inside;
}
fn_inside = outside(3); // Think of it like: give me a function that adds 3 to whatever you give
                        // it
result = fn_inside(5); // returns 8

result1 = outside(3)(5); // returns 8

Answer

You’re not returning your function, what you probably want to do is:

function A(x) {
  function B(y) {
    function C(z) {
      console.log(x + y + z);
    }
    return C;
  }
  return B;
}

Or, using function expressions:

function A(x) {
  return function B(y) {
    return function C(z) {
      console.log(x + y + z);
    };
  };
}


Source: stackoverflow