Hi all, I want to know what is name space object? And also for what we will use that... can any one explain me... Thanks in advance....
A namespace is basically a container for all your methods and objects in a way it's very similar to classes in other languages. The best way to understand it is to look at some existing code - http://stackoverflow.com/questions/881515/javascript-namespace-declaration
also, the reasons for the namespacing / using a singleton model are a few. first of all, it's neater and easier to understand. if you have a single 'site' object, then all functions and variables within that can be pertaining to the website. it allows sharing of data between methods via the this construct (if bound correctly). for instance, you may do: var done = true; // global window.done ... if (done) { // what was done? oh yeah, global... } // vs var site = { done: false, initBox: function() { if (this.done) { // you know its your site namespace. } } }; site.initBox(); site.done = true; site.initBox(); if (site.done) { ... } Code (javascript): it avoids having any global variable conflicts which can sometimes happen in loops when not using var or as a result of careless ajax or most importantly, the inclusion of a 3-rd party script that has also decided to define a window.done variable that overwrites yours. one way to work around the shortcomings of sharing code and variables is to create a closure (js has no if/then scope and does hoisting) - so something like: var done = false; (function() { // private variable var done = true; alert(done); // true; })(); alert(done); // false Code (javascript): still, one of the biggest reasons you'd want to namespace is performance. google up 'javascript scope chain' and you will find out why but it has to do with how javascript will try to work out if a variable exists recursively each time you reference it from any function. this is why is is also very expensive to add many window variables - they are looped through and accessed from all functions that reference a non-specific global variable... scope is defined as the set of variables that each function/pseudo block scope has available to it (locally defined variables), then the prototypical chain, then the parent function and so forth until it reaches window. variables. so a case where you do: var foo = 1; var foofunc = function() { var barfunc = function() { alert(foo); }; barfunc(); }; foofunc(); Code (javascript): ... will result in the interpreter looking for foo within barfunc hoisted variables, barfunc prototypical chain, then foofunc vars, foofunc prototypes and finally window.foo. in such instances its advisable to shorten the chain by using window.foo or using a local copy of foo or a singleton (which helps somewhat). I would hesitate to use a deep level singleton with properties like site.window.options.height as it can create a performance hit as well. anyway, good luck and have fun. javascript is awesome but quirky and i hope to learn it properly someday if you like OOP, look up the mootools javascript framework which abstracts all the class stuff nicely for you with proper inheritance - extending classes or using mixins, mutators, decorators and whatnot.