DEV Community

Discussion on: TypeScript vs JavaScript🤔

Collapse
 
joelbonetr profile image
JoelBonetR 🥇 • Edited

Namespaces are a paradigm and not a "feature".

var truck= {
  start: function () {
    console.log("Truck in Started!");
  }
}

var bus = {
  start: function () {
    console.log("Bus is Started!");
  }
}
Enter fullscreen mode Exit fullscreen mode

Start method is scoped inside it's object (context).

truck.start(); // Truck in Started!
bus.start(); // Bus is Started!
Enter fullscreen mode Exit fullscreen mode
  • Namespace usage protects and isolates the code from other applications.
  • You can use multiple functions with the same name, then distinguish them by utilizing the JavaScript namespace like in the example above.
  • It becomes easier to recognize the variables and functions from where they are defined.

It's not the only way to reach that. Setting private functions for example, won't clash against other private ones so they are contextualized separatelly be they with the same name or not.
The difference with the namespaces are that you mark a data structure (Object) property as the method so you always call it through the instantiation object name plus the method name.