if(true) { (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){ (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o), m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m) })(window,document,'script','https://www.google-analytics.com/analytics.js','ga'); } if (typeof ga === "function") { ga('create', 'G-EZ0B2QJ2Z6', 'auto', {}); }Understanding Namespaces in JavaScript | RAHULISM

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 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)

© 2020 RAHULISM • Developed with ❤️ and 🍕 by RAHUL