+ 1
Why i am unable to use global variable in other function?
https://code.sololearn.com/W05g1uL446qT/?ref=app Anyone help me what i am doing wrong in this?
9 Answers
+ 2
1. Fix
https://code.sololearn.com/WdgCK0Q6Q0Qa/?ref=app
2. Your mistakes:
2.1 Variables declared in local scope of anonymous function instead of global scope.
2.2 a typo in client function.
3. Readings:
3.1 https://www.sololearn.com/post/45261/?ref=app
3.2 https://code.sololearn.com/WdGAFeNRAe9M/?ref=app
+ 3
BroFar Yes that works too. It is a shortcut.
It doesn't out declare the variables in the global scope. Instead it moves the function into local scope of anonymous function of window.onload.
If this way is adopted, all other functions will have to be declared in the same local scope.
Using closure is encouraged because it avoids name contamination when code base grows.
Here is a reading about JavaScript Design Patterns:
https://blog.bitsrc.io/understanding-design-patterns-in-javascript-13345223f2dd
+ 2
Thanks Gordon for helping me
Thank you so much!
+ 2
You are welcome, I just edited to include two relevant readings, take time read them.
Here are some more readings, if you have time:
https://code.sololearn.com/Wyr76080kKxS/?ref=app
https://code.sololearn.com/WL8P7nb3aq6L/?ref=app
https://code.sololearn.com/WKFF4vOCr3mi/?ref=app
https://code.sololearn.com/WKKkpq0efxai/?ref=app
Keep up đȘ
+ 2
Gordon I just changed his upper global closing bracket } to the bottom and same result.
I believe you are write on the rest...
https://code.sololearn.com/W3OlTUslHZ6I/?ref=app
+ 2
Wait, a ping pong game, so you may find this tutorial series by Happy To Help a useful readings:
https://code.sololearn.com/WvIsS7qN8npX/?ref=app
+ 2
Thanks Gordon đ€ nice to know
+ 2
Thanks to sharing this stuff i will find time to read this
+ 2
Alternative
https://code.sololearn.com/WGtogsM2Jh4Q/?ref=app