We were unable to load Disqus. If you are a moderator please see our troubleshooting guide.

thomasseandominickelly • 2 years ago

there is a slight error in the code example in the buildname() code block:

the sayname() method on line 5 should console.log the variable built in that scope
eg. console.log(welcome) as opposed to console.log(greeting)

Eloise • 3 months ago

And there is also an error in the buildContor example: the console.log must be contor and not contor++
is this something done in purpose or what? Two first examples => two errors in the result.
While it is already complicated to understand the concept of closure !

Guest • 3 months ago
colin trinkley • 3 months ago

thanks

Farhan Ali • 1 year ago

myContor(); // 1
myContor(); // 3
myContor(); // 5
The output you mentioned were 1,2,3.

gabrielsoft • 6 months ago

Done Justice to the topic

robrecord • 1 year ago

The article linked at the top of this article (under Prerequisites) has moved to https://dacianco.wordpress....

Simsima • 1 year ago

Through getVar and setVar, I can manipulate the myVar property but I don’t have direct access to it.
I was able to access idirectly...am I missing something?

obj.myVar = 5;
console.log(obj.myVar); //5

Georges • 7 months ago

No you didn't access the myVar variable that is INSIDE the closure, but instead by writting
obj.myVar = 5; you have set a new property called "myVar" on the object itself.

If you try to console.log(obj.getVar()); you will not get 5 but the value of the "myVar" that is inside the closure. This means you didn't change this one.

Andrii Androsovich • 1 year ago

It`s a great Explanation for beginners!

LuisAlvarado • 1 year ago

Excellent explanation. Actually the best. I found a couple of typos.

Please change the following sentences:

-------
If any variable is change in the outer scope, than the change will be visible in the next call as well.

To this:

If any variable is change in the outer scope, then the change will be visible in the next call as well.

------
All three invocation of the function myContor are accessing the same outer scope, therefore the same contor variable. If one of them change it, the next call will se the value changed.

To this:

All three invocations of the function myContor are accessing the same outer scope, therefore the same contor variable. If one of them changes it, the next call will see the value changed.

------
myOtherContor() access a totally different variable.

To this:

myOtherContor() accesses a totally different variable.

--------
I hope you agree with me by now: Closures are not hard to understand if you are looking at them in the right perspective.

To this:

I hope you agree with me by now: Closures are not hard to understand if you are looking at them from the right perspective (Or angle, whichever you want to use).

Vincent Cheng • 1 year ago

Clear and hit-the-core explanation, appreciate the clarification!

spelling error • 2 years ago

will ??se?? the value changed.
also:
it the variables from the outer scope are changed, it will affect the following calls
should be:
if the variables from the outer scope are changed, it will affect the following calls