It looks like you are calling a function and storing the call to that function in a variable.
Why are the outcome different?????
I thought the results would be the same, why are they different?
It looks like you are calling a function and storing the call to that function in a variable.
Why are the outcome different?????
I thought the results would be the same, why are they different?
5
Answers
createCount()
returns function andcount()
returns value becausecount
is a function that is returned bycreateCount()
.Remember that
createCount
returns a function object. So this:Just prints the function object. It does not CALL the function that was returned. Where as this:
Stores the function object, and then CALLS that function object. Two very different operations.
It’s like the difference between these two:
I’ll give you some code that makes intuitive sense.
The code above is like
The code below is like
As you indicate this is an example of closures where a function has access to values in the outer scope, as long as they aren’t shaded by other functions.
I think this example is a lot simpler to get a first understanding:
So surname is already replaced.
You could also use other variables form the outer scope like
process.env.SURNAME
in a NodeJS process.Look at this code…