Docs of functions: is this really a closure? #588
-
Hey everyone, I was reading the docs of functions in JavaScript and I ran into this example: function addSquares(a, b) {
function square(x) {
return x * x;
}
return square(a) + square(b);
}
a = addSquares(2, 3); // returns 13
b = addSquares(3, 4); // returns 25
c = addSquares(4, 5); // returns 41 The docs say that the inner function
As far as I know, closures are created only if the nested function is referencing variables of the outer function. However, in the above example, I was going to open a PR for this, but I wanted to make sure I understand it well. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
You should be correct - neither of these functions enclose anything from an outer scope. |
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
You should be correct - neither of these functions enclose anything from an outer scope.
It is correct in that you could call the outer function from it, but as it does not reference the function within the body, it does not close over it.