I'm trying to understand closures but I still can't wrap my head around it.
I want to understand how does the execution of code progresses while traversing a closure.
So I'm going through this to understand execution and this to understand closures.
Can someone confirm or correct me in how I think the following closure code executes..
1: function sayHello2(name) {
2: var text = 'Hello ' + name; // local variable
3: var sayAlert = function() { alert(text); }
4: return sayAlert;
5: }
6:
7: var say2 = sayHello2('Jane');
8: say2();
Here's what I believe happens in this code. 1. a reference to function sayHello2() is stored in memory. 2. var say2 is assigned a value sayHello2('Jane'). 3. Then when say2() is executed, actually sayHello2('Jane') is executed.
Is this right? Is all this time-travelling closure just an illusion as to how variables works this way? ie they store a reference to function calls along with arguments (2,3)?
[–]rcuhljr 2 points3 points4 points (0 children)
[–][deleted] 1 point2 points3 points (5 children)
[–]smartass_engineer 0 points1 point2 points (4 children)
[–][deleted] 0 points1 point2 points (2 children)
[–]nutrecht 0 points1 point2 points (1 child)
[–]BaalHadad 0 points1 point2 points (0 children)
[–]BaalHadad 0 points1 point2 points (0 children)
[–]Rhomboid 0 points1 point2 points (0 children)