Creating a nested function, and then attempting to fill in all function parameters results in an error:
JavaScript
x
14
14
1
function A(x) {
2
function B(y) {
3
function C(z) {
4
console.log(x + y + z);
5
}
6
}
7
}
8
9
A(2)(2)(2);
10
11
12
13
>> Uncaught TypeError: A( ) is not a function
14
However on the MDN documentation, a nested function such as the following works correctly:
JavaScript
1
12
12
1
function outside(x) {
2
function inside(y) {
3
return x + y;
4
}
5
return inside;
6
}
7
fn_inside = outside(3); // Think of it like: give me a function that adds 3 to whatever you give
8
// it
9
result = fn_inside(5); // returns 8
10
11
result1 = outside(3)(5); // returns 8
12
Advertisement
Answer
You’re not returning your function, what you probably want to do is:
JavaScript
1
10
10
1
function A(x) {
2
function B(y) {
3
function C(z) {
4
console.log(x + y + z);
5
}
6
return C;
7
}
8
return B;
9
}
10
Or, using function expressions:
JavaScript
1
8
1
function A(x) {
2
return function B(y) {
3
return function C(z) {
4
console.log(x + y + z);
5
};
6
};
7
}
8