Understanding Namespaces in JavaScript
JavaScript is the tough thing all the beginners find and i find it too. So here is a simple post on Namespace in javascript.
What is Namespace ?
Namespace is a container for set of identifiers, functions, methods, variables etc. It gives a level of direction to its contents so that it will be well distinguished and organized.
Why is Namespace needed ?
We need because it doesn’t allow us to pollute our code base and makes it cleaner by grouping our code logically and avoiding unexpected and expected collision.
Now you know the basics. Let's see how you can create it.
Creating a Namespace in JavaScript
const bmw = {
start: () => {
console.log('start')
},
stop: () => {
console.log('stop')
}
}
start
andstop
are identifiers.- So, by this way, start and stop are namespaced under
bmw: bmw.start()
andbmw.stop()
.
As you can see they aren't polluting the global object. Nothing can interfere with them 👊.
Thanks For Reading.
(Knowledge Under 1 min. MORE ON WAY)