Even though JavaScript lacks namespaces, we can use Objects , IIFE to create namespaces.
var namespaceOne = { function func1() { console.log("This is a first definition"); } } var namespaceTwo = { function func1() { console.log("This is a second definition"); } } namespaceOne.func1(); // This is a first definition namespaceTwo.func1(); // This is a second definition
(function () { function fun1() { console.log("This is a first definition"); } fun1(); })(); (function () { function fun1() { console.log("This is a second definition"); } fun1(); })();
{ let myFunction = function fun1() { console.log("This is a first definition"); }; myFunction(); } //myFunction(): ReferenceError: myFunction is not defined. { let myFunction = function fun1() { console.log("This is a second definition"); }; myFunction(); } //myFunction(): ReferenceError: myFunction is not defined.