DEV Community

Discussion on: TypeScript vs JavaScriptπŸ€”

Collapse
 
iarchitsharma profile image
Archit Sharma

This kind of thing is readily available for JS also.

well it says including...

Not entirely sure what you mean here, but modules are fully available natively in JS.

In JavaScript there are no predefined methods to use namespaces.

Collapse
 
jonrandy profile image
Jon Randy πŸŽ–οΈ

If they both 'include' it, how is it an advantage?

Thread Thread
 
iarchitsharma profile image
Archit Sharma

Actually main point over here was -

Excellent tools support

Although I get it what you wanna say
Thanks for fact checking!

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.