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

thomasseandominickelly • 3 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 • 1 year 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 • 1 year ago
colin trinkley • 1 year ago

thanks

Farhan Ali • 2 years ago

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

Caillou09 • 1 year ago

One of the best article on Closure that I have ever read. Thank you !!! πŸ™ πŸ™ πŸ™

gabrielsoft • 2 years ago

Done Justice to the topic

robrecord • 2 years ago

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

Simsima • 2 years 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 • 2 years 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 • 2 years ago

It`s a great Explanation for beginners!

LuisAlvarado • 3 years 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 • 3 years ago

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

spelling error • 4 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