In Javascript, what\'s the difference between a namespace and a closure? They seem very similar to me.
EDIT
Specifically, this article discusses namesp
A namespace is typically a method of putting all your global variables as properties under one master global variable, thus only adding one new truly top-level global variable. It prevents pollution of the global namespace and reduces the chance of conflict with other global variables.
And example of a namespace:
var YUI = {};
YUI.one = function(sel) {...};
YUI.two = function(sel) {...};
YUI.three = function(sel) {...};
There is one new item in the top-level global namespace YUI
, but there are multiple globally accessible items via the YUI namespace object.
A closure is a function block that lasts beyond the normal finish of the execution of the function because of lasting references to internal parts of the function.
function doSometing() {
var x = 10;
setTimer(function() {
// this gets called after doSomething() has finished executing
// but because of the function closure, the variables
// inside of the parent scope like x are still accessible
x += 10;
}, 1000);
}