Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialGordon Freeman
Courses Plus Student 930 PointsWhy can't we just use global variables Instead of State? then I think we don't need to pass all the data around.
If I just use global variable or object which contains players and counter then wouldn't it be much easier for them(components) to just access it without passing arguments? There must be a good answer for this. please enlighten me!
2 Answers
Ignazio Calo
Courses Plus Student 1,819 Pointsyou're facing a true problem when it comes to design the architecture of an app. At the first look you think that a global variable would make the app way more easy, but i you google a bit you'll see that everyone says "please do not use global variables".
There are some "official approved" explanations why global variables are bad here: http://wiki.c2.com/?GlobalVariablesAreBad Additionally with JS you can have some additional troubles caused by the language itself (performances and overriding).
In few words, in a small app a global variable is easier to use but as soon the app grows it's hard to keep track of all the pieces of the app that edit the variable making code less easy to read/maintain
Jesse Thompson
10,684 PointsI kind of wondered this too and Im glad this question was asked. Thanks Ignazio for the response.
Gordon Freeman
Courses Plus Student 930 PointsGordon Freeman
Courses Plus Student 930 PointsThank you so much!