# Explain how the circuit works?

Hello!
Explain how in this case the circuit works, but something does not understand
``````function celebrityIDCreator (theCelebrities) {
var i;
var uniqueID = 100;
for (i = 0; i < theCelebrities.length; i++) {
theCelebrities[i]["id"] = function (j) {
return function () {
return uniqueID + j;
} ()
} (i);
}

return theCelebrities;
}

var actionCelebs = [
{name:"Stallone", id:0},
{name:"Cruise", id:0},
{name:"Willis", id:0}
];

celebrityIDCreator (actionCelebs )``````

Thank you very much!
June 14th 19 at 18:27
June 14th 19 at 18:29
Solution
I understand the circuit so: "child" function has access to variables of the function "parent".
In other words, the variable "uniqueID" declared inside functions "celebrityIDCreator", and thus in the global scope the variable no. In a cycle fulfills the anonymous function (line 5 in the code), it is possible to say that the "child". But this anonymous function has access to variable "uniqueID"! Although variable and not global, and was not declared inside the anonymous function!
How Vliet what I anonymous f-tsii i pass argument?? - ludwig33 commented on June 14th 19 at 18:32
The value of i is substituted in instead of j. - Lauren.Bay commented on June 14th 19 at 18:35
By the way, inside the first anonymous function is the second anonymous function. She has access to the variable j, although the scope of it as it is on the level above)) In your example a few circuits - Lauren.Bay commented on June 14th 19 at 18:38
, Uhh...head breaks) - ludwig33 commented on June 14th 19 at 18:41
June 14th 19 at 18:31
You `uniqueID` use inside the function but it is declared outside visibility of this feature
``````function (j) {
return function () {
return uniqueID + j;
} ()
} (i);``````

this is the circuit.

Pro circuit is well here described.
Or contact Soshnikova: dmitrysoshnikov.com/ecmascript/chapter-6-closures - ludwig33 commented on June 14th 19 at 18:34
June 14th 19 at 18:33
In this example, the variable actionCelebs available in celebrityIDCreator().
actionCelebs available in celebrityIDCreator() as theCelebrities because the array passed as an argument. But the question was about circuits... - ludwig33 commented on June 14th 19 at 18:36
June 14th 19 at 18:35
Why circuit such nested functions in this case ?

``````function celebrityIDCreator (theCelebrities) {
return theCelebrities.map((celebrity, index) => {
celebrity.id = 100 + index;
return celebrity;
})
}``````

not ?

Find more questions by tags JavaScript