How to prevent overriding using fake namespace in JavaScript ?

Overriding is basically when you define multiple functions or variables that have the same name, the last one defined will override all the previously defined ones and every time when you invoke a function, the last defined one will get executed. Overriding usually happens when you have multiple javascript files in your page. It can be an external file or the file which you created internally. This usually occurs due to the presence of variables or functions with the same name in the global scope.

Suppose we have following files:
Filename: index.html

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
<head>
    <title>Namespace</title>
    <script src="script1.js"></script>
    <script src="script2.js"></script>
    <script src="app.js"></script>
</head>
<body></body>
</html>

chevron_right


This file has linked script1.js, script2.js and app.js as external javascript files which are given below:
Filename: script1.js

filter_none

edit
close

play_arrow

link
brightness_4
code

var name = "Arsalan";
function sayHello(){
    console.log("Hello "+name);
}

chevron_right


Filename: script2.js

filter_none

edit
close

play_arrow

link
brightness_4
code

var name = "John";
function sayHi(){
    console.log("Hi " + name);
}

chevron_right


Filename: app.js



filter_none

edit
close

play_arrow

link
brightness_4
code

sayHello();
sayHi();

chevron_right


Here app.js is responsible for invoking functions inside script1.js and script2.js and you will get below output in your console window as shown below:

Well, this happens because we have the same variable name in script1.js and script2.js in the global scope. This is the reason script2.js is overriding over script1.js

In order to fix this issue we will use the concept of the namespace as described below with the following changes made to these files:

Filename: index.html

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
<head>
    <title>Namespace</title>
    <script src="script1.js"></script>
    <script src="script2.js"></script>
    <script src="app.js"></script>
</head>
<body></body>
</html>

chevron_right


Filename: script1.js

filter_none

edit
close

play_arrow

link
brightness_4
code

var arsalanGreeter = {};
arsalanGreeter.name = "Arsalan";
arsalanGreeter.sayHello = function(){
    console.log("Hello "+ arsalanGreeter.name);
}

chevron_right


Filename: script2.js

filter_none

edit
close

play_arrow

link
brightness_4
code

var johnGreeter = {}
johnGreeter.name = "John";
johnGreeter.sayHi = function(){
    console.log("Hi " + johnGreeter.name);
}

chevron_right


In the above two files, we have created an object and then assigned names to the name variable. In this way, we have prevented any overriding issue.
Filename: app.js

filter_none

edit
close

play_arrow

link
brightness_4
code

arsalanGreeter.sayHello();
johnGreeter.sayHi();

chevron_right


In the above file, we have used a new function name and that will get invoked while running program and your console window will look as shown below:

full-stack-img




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.