Introducing namespaces in JavaScript

by Jagadish Pulakhandam on 9/25/2012 3:58:14 PM
Rated 0 from 0 votes
Brief: Demonstrates on defining a simple namespace to hold/separate functions from other libraries
Posted to: Advanced JavaScript Programming/Development tutorials/videos- Object Oriented Programming
Add to DiggAdd to del.icio.usAdd to FURLAdd to RedditAdd to YahooAdd to BlinklistAdd to GoogleAdd to ma.gnoliaAdd to ShadowsAdd to Technorati

Attached source code/video demonstrates the following:
  • How to define a simple object which behaves like a namespace in JavaScript
  • How to add functions to the namespace (hide functions from outside world/libraries)
  • How to access or call or execute functions available in JavaScript namespaces.
Source Code:

01.<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
03.<html>
04.<head>
05.    <title></title>
06.    <script type="text/javascript">
07. 
08.        //DEMO: A simple namespace to hold functions
09. 
10.        //classic way
11.        //----------------------
12.        //function ShowMessage(msg) {
13.        //    alert(msg);
14.        //}
15.        //or another way
16.        //ShowMessage = function(msg) {
17.        //    alert(msg);
18.        //};
19. 
20.        //ShowMessage("some message");
21. 
22. 
23.        //encapsulating function in a namespace
24.        SampleLibrary = {
25.            //this function will be hidden/separated from other JavaScript libraries
26.            ShowMessage : function(msg) {
27.                alert(msg);
28.            }
29.        }
30. 
31.        SampleLibrary.ShowMessage("another message");
32.    </script>
33.</head>
34.<body>
35. 
36.</body>
37.</html>



Rated 0 from 0 votes ( login  to rate)
DotnetKicks DotnetKicksDe DotNetShoutout

Attachments / Source Code
You need to Login or Join for FREE to download the following


Discussion
No comments yet!