3 ms·
You can use Object's as name-spaces, it's somewhat common to use var global = {}; and then put all global variables in it. But I'm a big fan of scoped imports a
by z3t4 8y ago
You can use Object's as name-spaces, it's somewhat common to use var global = {}; and then put all global variables in it.
But I'm a big fan of scoped imports aka. require in Node.JS. Scoped import is like a global variable, but with the complexity of a local variable.
I prefer function scope as it lets me declare variables where ever I want, but others prefer block scope, so either choose var or const and stick to it.
JavaScript was not the only contender for the browser, Microsoft tried with vbScript, and Google tried with Dart. Then there was ActionScript (flash) and Java (applets). And also ActiveX. Then there are various compile to JS languages, like CoffeeScript, yet plain old JavaScript still seem to be the most popular.
My own pain point with JavaScript is that it's getting more and more complicated. Personally I like to use the bare minimum when it comes to language features. And work with the language instead of against it, eg. I make use of null==undefined, I almost never use === unless I need to be explicit.
Learn the Equality-Table! Also learn regular expressions! (but don't over-use them). Learn how to use closures, learn how to use callbacks. Then it becomes enjoyable. Bonus: Learn how to program without global variables, then use them when it's convenient. Learn how to program with only pure functions, then use impurity for convenience. Learn test-driven-development, but only write tests where they're needed. Learn how to program without an IDE (just plain text editor and command prompt), then go back to the IDE. All the great tools that exist today make you not feel the pain when writing unmaintainable code.