While the notion of scope is a fundamental part of programming, I keep running across new ways the concept is applied, especially with the plethora of Javascript namespacing techniques that exist.
This weekend, I captured my thoughts on the subject in an article about scope and namespacing in Javascript.
Scope Your Javascript Variables Like a Pro
The first part of the article introduces the idea of global and local scopes, with a few examples of how let
and var
, the two ways to initialize a JS variable, handle scope differently.
After this, the second part delves into namespacing. Here, I ​include examples of static namespacing using direct assignment and object literals, as well as dynamic namespacing using immediately invoked function expressions (IIFEs) and the module design pattern.
There are many other namespacing variants that you will run across by studying Javascript frameworks and libraries. In addition to introducing the concepts of scope and namespacing, I hope that this article will be a good starting point towards learning more about how developers scope their frameworks and libraries to make them modular and compatible with the apps and websites that would use them.
Thanks for reading!
Top comments (0)