Answer well in turn:
First, why is it var myName = (function() { rather than var myName = (function(x) {
In this case, since the value x not required to create the return function. The array data structure contained in the returned function is hard-coded, so you do not need additional information to create it. Just like
function a() { return 1 + 2; }
There are no arguments because the values ββare hard-coded. This is an example of functions as data or first class functions.
Question two: var myArray is executed every time.
In short it is not. myArray was assigned at this point, so it has a value that the system has already calculated. In this case, it is a function. Miriam myArray is installed, so there is no reason to continue its execution. If the external function has not returned a new function, then yes, it will need to be called again and again, but this can defeat the purpose of this abstraction.
Question three: you can call an internal function.
No, thatβs out of scope. The whole point of this example is that you have defined a function generator.
source share