Difference between namespace and closure?

In Javascript, what's the difference between namespace and closure? They seem very similar to me.

EDIT

In particular, this article discusses namespaces and closures, as well as sentences such as

Now you still had situations when you want to declare variables that usually do not fit into the composition namespace object. But we do not want these variables to have global coverage. This is where self-starting functions appear.

He then gives what looks like a closure, like an “object namespace”. It seems to me that the IS namespace is closure ... but maybe it's not ...? Help?

+6
source share
3 answers

The namespace is essentially an Object without any interesting properties that you paste the material into so that you don't have a bunch of variables with similar and / or conflicting names running around your area. So for example, something like

 MyNS = {} MyNS.x = 2 MyNS.func = function() { return 7; } 

Closing is when a function “stores” the values ​​of variables that are not defined in it, although these variables are beyond the scope. Take the following:

 function makeCounter() { var x = 0; return function() { return x++; } } 

If I let c = makeCounter() and then call c() again, I will get 0, 1, 2, 3, ... This is because the scope of the internal anonymous function that makeCounter defines is “closed” over x , so it refers to it even if x is out of scope.

It is noteworthy that if I then do d = makeCounter() , d() will start counting from 0. This is because c and d get different instances of x .

+23
source

A namespace is usually a method of placing all your global variables as properties under one main global variable, so only one new top-level global variable is added. This prevents pollution of the global namespace and reduces the likelihood of conflict with other global variables.

And an example namespace:

 var YUI = {}; YUI.one = function(sel) {...}; YUI.two = function(sel) {...}; YUI.three = function(sel) {...}; 

There is one new element in the global top-level YUI namespace, but through the YUI namespace object there are several objects available around the world.

Closing is a function block that lasts above the normal completion of a function due to long references to the 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); } 
+5
source

From http://jibbering.com/faq/notes/closures/ :

A closure is formed by returning a function object that was created in the context of executing a function call from this function call, and assigning a reference to this internal function to a property of another object. Or by directly assigning a reference to such a function object, for example, to a global variable, a property of a global object or an object passed by reference as an argument for calling an external function.

Namespaces are simply conventions created by objects to avoid cluttering the global realm with variables.

+2
source

Source: https://habr.com/ru/post/914287/


All Articles