We were unable to load Disqus. If you are a moderator please see our troubleshooting guide.
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)
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 !
myContor(); // 1myContor(); // 3myContor(); // 5The output you mentioned were 1,2,3.
Done Justice to the topic
The article linked at the top of this article (under Prerequisites) has moved to https://dacianco.wordpress....
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
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.
It`s a great Explanation for beginners!
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.
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.
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.
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.
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).
Clear and hit-the-core explanation, appreciate the clarification!
will ??se?? the value changed.also:it the variables from the outer scope are changed, it will affect the following callsshould be:if the variables from the outer scope are changed, it will affect the following calls