I have a fairly good grasp of the concepts of scope and closures in JavaScript.
Moreover, the following sites provide examples of how JavaScript namespaces can be implemented:
What I still do not understand is how many people seem to mix the concepts of scope and namespace. Furthermore, the same people often also mention how one should not "pollute the global namespace" and not "create global variables / variables in the global scope".
Questions
let
/const
)bar
, avoiding to pollute the global namespace (except for foo
), but that bar
is still in the global scope: var foo = { bar: 42 }
A namespace is a mapping from names to objects . A scope is a textual region of a Python program where a namespace is directly accessible.
JavaScript Namespaces: Namespace refers to the programming paradigm of providing scope to the identifiers (names of types, functions, variables, etc) to prevent collisions between them. For instance, the same variable name might be required in a program in different contexts.
Scope in JavaScript refers to the current context of code, which determines the accessibility of variables to JavaScript. The two types of scope are local and global: Global variables are those declared outside of a block. Local variables are those declared inside of a block.
JavaScript has 3 types of scope: Block scope. Function scope. Global scope.
Is it not correct that scope and namespace are two completely different concepts?
I would not say that scopes and namespaces are completely unrelated. If we take the literal meaning of "namespace", it's a space of distinct names - unique and not colliding with names from other spaces, as you say.
A scope definitely forms a name space in that regard - the variable names are distinct and don't collide with names from other scopes. However, a scope is internal and can't be accessed from outside, so it's not particularly interesting for organising things.
An object also forms a name space - the property names are distinct and don't collide with names on other objects. This is why objects are used for structuring code into modules, and this is the traditional meaning of "namespace" in JavaScript.
Is it correct that the following object literal creates a new namespace for
bar
, avoiding to pollute the global namespace (except forfoo
), but thatbar
is still in the global scope:var foo = { bar: 42 }
No, bar
is not a member of the global scope. It's a property of some object.
Is it not wrong to say "don't create global variables so you do not pollute the global namespace"? Global vs local variables (scope) is different from namespacing.
Globals are a bit special in JS: they are both properties of the global object and variables in the global scope. That's why we talk of "the global namespace" to encompass all - and specifically to emphasise that the names must not collide.
If avoiding to pollute the global namespace is the only reason for why we should not create global variables, is it not enough to just create new namespaces and still keep it all global?
Creating namespaces, like {bar: 42}
in your example above, does still create global variables: foo
. It just creates fewer variables - one per module. Also it creates a certain naming convention, namely that global variables should refer to modules and not to mundane variables. We wouldn't use var i, increment, decrement;
but var counter = {i: …, increment(){…}, decrement(){…}};
, and refer to the purpose of them in the name counter
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With