DCSIMG
Saving the Global Namespace - Nir Tayeb

MS Israel Community

Building a community
Welcome to MS Israel Community Sign in | Join | Help
in Search

Nir Tayeb

JavaScript UI programming, Server side development, news and tools.

Saving the Global Namespace

Writing a code that need to work on a lot of websites/applications (for example: WordPress plugin) require us to ensure that the code is working in every site properly, and that our code isn't affect on the site code itself.

The code won't work if something outside our code change how the code works, by overriding function and variables. That can easily happen if we put our code in the global namespace.
We can accomplish this requirement by saving on our code's namespace, if we run our code in private namespace the code will not affect on the hosting site and backward. Every JavaScript programmer who has a few experience with JavaScript and functions know that variables which declared in the function aren't exposed outside the function. We can use this fact to our mission., if we  surround our code with anonymous function declaration and call immediately the function, we will achieve a private namespace, a world that is private to us, and only to us, nobody outside the function can affect it.

var x = “Hello World”;
(function(){
    var x = “Hello Anonymous World!!!”;
    alert(x); // alerts “Hello Anonymous World”
})();
alert(x); // alerts “Hello World”

So when we define a variable or function inside the anonymous function it won't override any variable/function outside.
To expose a object (variable, functions, in JavaScript anything is object) to the world (in other word register it in the global namespace) we can use another JavaScript features, each object property can called or created by his key as in Hash Tables. Each variable in the global namespace is actually a property of the global `window` object that exposed by the browser, and by use these features together we can expose the object outside to the large world:

(function(){
    var x = “Hello Anonymous World!!!”;
    alert(x); // alerts “Hello Anonymous World”
    window[“data”] = x;
})();
alert(data); // alerts “Hello Anonymous World”

You can see this technique a lot in JQuery plugins that need to work together without override each other variables/methods.
Another way to save the global namespace and exposing the functionality to the world, is by using a one variable, with unique name that will contains the functionality of the whole application. Like JQuery or Yahoo! UI does.

Comments

No Comments

Leave a Comment

(required) 
(optional)
(required) 

Enter the numbers above:
Submit

About Nir Tayeb

Web developer since 2004, I've experienced with PHP, Ruby/RoR, JSP-Servelts, ASP, JavaScript and HTML/CSS. I develop a lot with JavaScript using AJAX, advance DOM using and MooTools library for ease and fast developing. I published some articles for webmaster.org.il and web developer's forum in tapuz.co.il.
Powered by Community Server (Commercial Edition), by Telligent Systems