The reason is that it allows you to encapsulateturn JS into an oozy play-doh like substance. What the heck do I mean? Encapsulate all your variables within a named objestobject. Now they can maintain state and float around in your single global member "namespace". Why is this a flexible substance? I'll get to that in the answer to the second question, as they seem related. But befor now realize that this pattern allows JS to become highly transient: once modular.
Be warned, this is the beginning of Advanced JavaScript - not the end. Allowing one object to communicate with another namespaced objects is, yet another, advanced topic.
ToQuick Tangent:
*To keep theseobjects nicely modular objectsand decoupled, the solution is generally to send off events between an intermediary not direct references (it is so easy to do so), but it will hurt you in the long run between all objects. References become a maintenance-nightmare. Look up PubSub or evented viewsEvented Views for this. Don't worry it is worth it, as you watch the language expand vertically and horizontally through this pattern tangent.
Let me explain...here why flexibility may cause mixed-reaction.
Such a good question, worthy of a psychological or socialogical review. And we do notdont have time, but, more. More often than not, when speaking about Advanced JavaScript - for whatever reason... a strong negative reaction can occur. We have all experiencedHave you observed this in practice, yes?
First off, it may be that the original question is inflammatory,
- it may be that the original question is inflammatory,
The other possibility is that, potentially, there is a negative reaction external to your question. This is what is interesting. Why?
- The other possibility is that, potentially, there is a negative reaction external to your question. Wait that is strange? What? Why?
There seems to me, an unspoken stigma. A misconception. Anyone know what I am talking about - have you observed this? Something, somehow embedded rection, somewhere .- there is a n embedded reaction.. or so. And this is what I see with JS. It has a tangled history with Sun naming similar to, confused with Java,... misunderstood as a 'Toy-Language' and yadda yadda yadda,
(look up Enter the Dragon - Dmitry Baronovski) butThat said, there is another > point I
wish to raise here that is far more important and that is.... flexibility.
Flexibility
JavaScript flexibility causes... different perspectives.
JavaScript is very flexible language. No? It is so flexible, to the point, that it can be squeezed out one hole and become a server(node.js), another and become a datbase (MongoDB), another and become MVC (backbone) and yet another and become a rich enterprise framework (ExtJS or jQuery)... and don't forget Mobile; phonegap, grunt, or three.jsBuild: grunt. This is remarkable. It is like play-doh. Yes a toy-language indeed.
So now lets consider how JS exists in CoffeeScript, or GWT, Dart, or Windows 8. They are remarkably different beasts.
This comes full circle, now, to the very first controversial point raised earlier that was required here to be made on "functional programming" and how it,conversation might be, in "distracting". In all respect, "distracting"let me clarify, "functional programming", while it can be defined at wikipedia - mehas a very good chance of a spudo-philosophical meaning that ends up meaning something slightly different to all of us.
The fact is, JavaScript can be seen in so many different ways (paradigms), that
we should actually try, at some point, think about viewing it through different lenses.
JS exists in many context domains, it is a point of significant growth, and will be even moreso in the future.
It is so flexible that it can be understood in completely separate ways.
The problem thoughhere, with this, is us. Honesty is the best policy here and here is my humble pie. I callThe problem is that it 'the metaphore invisibleis an 'invisible fruit'. Please bear with me, I make sense - eventuallyhave a point...
Invisible-Fruit
The notion is this: we all like our programmatic fruit, but. But code is, essentially invisible - because it cannot be consumed/digested/conceptualized, digested, conceptualized... in a single glance or sitting. It must be consumed. So we tend to go... with the fruit thatwhat we know. And yesThis is primarily because, often times we bite into a pit, sometimes an orange-peel, and in the most unfortunate of occasions - a lump of coal.
It is for this meansreason that if some other personstranger hands you their invisible fruit, saying, check this out it is great... thereno matter what they may say - you are thesuspicious. Aren't you?
There are two options: 1 of agnosticism, or2 of aversion. Because, hey wait! This is not my fruit!
In order to intellectually consume the new programmatic paradigm, one must make a blind leap-of-faith and - "take a bite". Do you observe this is in practice?
But more often than not, because we are biting-into a lump of code that is beneficial to another engineer in another context - but is just not beneficial to us.
Do you know what I mean? We see this often when some refuse to take a bite of code methodology because, perhaps in
Did you ever fervently consider the pastnotion, they have been burnedwhich is better: jQuery, ExtJS, YUI, Dojo, MooTools, Backbone, Underscore, Require, etc. I have in Clarion & Magik..
The take-away is this.
JavaScript is so malleable (sorry uber-fansthrough the advanced code that begins with the namespace pattern) it can transcend technologies in a way that makes JS not really better or worse implementations, but just different tools for a different jobs. And some are a better fit than others. I still hear many conversations accidentally fall into this.
The take-away is this. Perhaps, we will need tocan -eventually- begin to think about lowering our defenses,. in this language of the web - this multi-paradigm, meta-morphing language, our old pal JavaScript - and see it through separate lenses.
Time and again we find, the solution depends on the context. But time
and again we defend doggedly persistently- our minority experience.
JavaScript is larger than any one of us and it will eventually outgrow capability to fully consume/digest/understand it.