Namespaces in JavaScript

Veröffentlicht am: 03 February 2015
auf dem Kanal: kudvenkat
61,101
328

Link for all dot net and sql server video tutorial playlists
   / kudvenkat  

Link for slides, code samples and text version of the video
http://csharp-video-tutorials.blogspo...

Healthy diet is very important both for the body and mind. If you like Aarvi Kitchen recipes, please support by sharing, subscribing and liking our YouTube channel. Hope you can help.
   / @aarvikitchen5572  

In this video, we will discuss how to use namespaces to avoid polluting the global scope. This is continuation to Part 55. Please watch Part 55 of JavaScript tutorial to understand the problem of global namespace pollution.

JavaScript lack namespaces. However we can use objects to create namespaces.

The following line says use the PragimTech object if it already exists, otherwise create an empty object.
var PragimTech = PragimTech || {};

The following line adds a nested namespace. A nested namespace is a namespace inside another namespace. In JavaScript to define a nested namespace, you define an object inside another object.
PragimTech.TeamA = PragimTech.TeamA || {};

Modify the script in TeamA.js file as shown below. In this example we are adding customer() function to PragimTech.TeamA namespace.

var PragimTech = PragimTech || {};
PragimTech.TeamA = PragimTech.TeamA || {};

PragimTech.TeamA.customer = function (firstName, lastName)
{
this.firstName = firstName;
this.lastName = lastName;

this.getFullName = function ()
{
return this.firstName + " " + this.lastName;
}

return this;
}

PragimTech will be added to the global namespace. window is the alias for global namespace in JavaScript. You can now access customer() function as shown below.

PragimTech.TeamA.customer("Tom", "Grover")

OR

window.PragimTech.TeamA.customer("Tom", "Grover")

Modify the script in TeamB.js file as shown below. In this example we are adding customer() function to PragimTech.TeamB namespace.

var PragimTech = PragimTech || {};
PragimTech.TeamB = PragimTech.TeamB || {};

PragimTech.TeamB.customer = function (firstName, middleName, lastName)
{
this.firstName = firstName;
this.middleName = middleName;
this.lastName = lastName;

this.getFullName = function ()
{
return this.firstName + " " + this.middleName + " " + this.lastName;
}

return this;
}

PragimTech will be added to the global namespace. You can now access customer() function as shown below.

PragimTech.TeamB.customer("Tom", "T", "Grover")

OR

window.PragimTech.TeamB.customer("Tom", "T", "Grover")

On any given HTML page you should be able to access both the versions of customer() function as shown below.

[html]
[head]
[script type="text/javascript" src="TeamA.js" ][/script]
[script type="text/javascript" src="TeamB.js" ][/script]
[/head]
[body]
[script type="text/javascript"]
// Call the customer function that is defined in TeamA.js
alert(PragimTech.TeamA.customer("Tom", "Grover").getFullName());

// Call the customer function that is defined in TeamB.js
alert(PragimTech.TeamB.customer("Tom", "T", "Grover").getFullName());
[/script]
[/body]
[/html]


Auf dieser Seite können Sie das Online-Video Namespaces in JavaScript mit der Dauer stunde minuten sekunde in guter Qualität ansehen, das der Benutzer kudvenkat 03 February 2015 hochgeladen hat, den Link mit Freunden und Bekannten teilen, dieses Video wurde auf Youtube bereits 61,101 Mal angesehen und es wurde von 328 den Zuschauern gefallen. Viel Spaß beim Betrachtenden Zuschauern gefallen!