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
.
source share