Rahul
RAHULISM

RAHULISM

Understanding Namespaces in JavaScript

Understanding Namespaces in JavaScript

Rahul
·Nov 16, 2020·

1 min read

Subscribe to my newsletter and never miss my upcoming articles

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?

A namespace is a container for a 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 it because it doesn’t allow us to pollute our codebase and makes it cleaner by grouping our code logically and avoiding an 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 and stop are identifiers. 
  • So, by this way, start and stop are namespaced under bmw: bmw.start() and bmw.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)

 
Share this