I agree that using a namespace is essential for JavaScript libraries, but what about those functions that reside in HTML files in a script
tag? Basically these are "local" functions that are never shared with any other pages. Do you guys still use a namespace for those? If so, do you use the same namespace as your shared custom library?
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.
JavaScript does not provide namespace by default. However, we can replicate this functionality by making a global object which can contain all functions and variables.
In JavaScript, it is nothing but a single global object which will contain all our functions, methods, variables and all that. Here ' MYAPPLICATION ' is acted as a JavaScript namespace and the only global object which contains all other items.
A JavaScript function is defined with the function keyword, followed by a name, followed by parentheses (). Function names can contain letters, digits, underscores, and dollar signs (same rules as variables). The parentheses may include parameter names separated by commas: (parameter1, parameter2, ...)
I would say that a local namespace can help guard against any possible collisions with 3rd party JavaScript libraries, but it need not be imperative.
You don't need a namespace, because as you say, there is no potential naming conflict.
The only potential conflict is if you are importing a library that does not use namespaces, and it introduces a variable which conflicts with a name you placed in the script file. But if you are following the namespaces practice in all the libraries, then it doesn't matter.
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