简体   繁体   中英

Is following snippet of code a namespace or closure

I know there are a lot of threads about namespaces in here already. And I also read lots of them. Still I am a bit unsure, rather got a bit screwed up about namespace versus closures. I also bring a some specific bit of code with my thread.

Following is a namespace:

 var Interface = (function(){
     return {
         name: value,
         name: function( ... ) {
         }
     }; })();


//interface.call(); >> to call the namespace (I did not get where yet, but this is another story I think)

and also:

var Interface = (function(){
     function a(){
     }
     return {
         name: a
     }; })();

Ok, reading back many threads and sites, first thing I noticed is: - there are no namespaces in Javascript, they were skipped for some reasons. - Closure seem to be pretty much the same, at least, I still have lack of knowledge to understand the difference. Here for example is a function which is supposed to be a closure:

var Counter = (function() {
  var privateCounter = 0;
  function changeBy(val) {
    privateCounter += val;
  }
  return {
    increment: function() {
      changeBy(1);
    },
    decrement: function() {
      changeBy(-1);
    },
    value: function() {
      return privateCounter;
    }
  }   
})();

Questions:

1) Is namespace and closure the same, and maybe just at different levels of "definition" like: The closure ("method") is HOW the namespaces (a concept) is realized in Javascript?

2) In above mentioned namespace: Would I access function a via the following?

Interface.name()

3) How would this namespace work, with having a function with parameters?

 var Interface = (function(){
     function a(p1, p2){
     }
     return {
         name: a
     }; })();

How could this be called? (Notice I inserted p1 and p2 ).

4) Is a namespace a (one!) namespace or is namespace maybe also a namespace which can include namespaces (like folders can have folders)?

Is namespace and closure the same

No. A namespace is fuzzily defined term for "Putting everything you do as properties of a object assigned to a single global variable".

A closure is a means of wrapping up some variables so they can only be accessed by a particular function (or set of functions).

In above mentioned namespace: Would I access 'function a' via following:

Interface.name()

Yes

How would this namespace work, with having a function with parameters?

The same as any other function. If you want to pass arguments, you put them between ( and ) when you call it.

Is a namespace a (one!) namespace or is namespace maybe also a namespace which can include namespaces (like folders can have folders)?

An object can have properties pointing to other objects. Those objects can do the same. And so on as deep as you like. Any object can be given a function as a property value.

1) Is namespace and closure the same, and maybe just at different levels of "definition" like: The closure ("method") is HOW the namespaces (a concept) is realized in Javascript?

That's pretty much it. Since "namespace" isn't a concept that exists in JavaScript, we emulate them with objects. A closure (that returns an object) is just one way of creating such an object.

2) In above mentioned namespace: Would I access 'function a' via following: Interface.name()

Yes, because name is a property of the Interface object, and it is assigned function a as a value.

3) How would this namespace work, with having a function with parameters?

Like this:

var Interface = (function(){
    return {
        name: function(p1, p2) {
        }
    }; 
})();

Or this:

var Interface = (function(){
    function name(p1, p2) {
    }
    return {
        name: name
    }; 
})();

How could this be called? (Notice I inserted p1 and p2).

Like this:

Interface.name(123, 'abc');

4) Is a namespace a (one!) namespace or is namespace maybe also a namespace which can include namespaces (like folders can have folders)?

Since a "namespace" is just an abstract concept in JavaScript, and is usually implemented as an object, it can have another nested "namespace," just have another object as one of its properties and call it a namespace.

Hello

First of all thank you for the answers. But actually as I read them, the answers mean the exact opposite for the question 'Is namespace and closure the same'.

First Answer: That's pretty much it. Since "namespace" isn't a concept that exists in JavaScript, we emulate them with objects. A closure (that returns an object) is just one way of creating such an object.

Second: No. A namespace is fuzzily defined term for "Putting everything you do as properties of a object assigned to a single global variable". A closure is a means of wrapping up some variables so they can only be accessed by a particular function (or set of functions).

Conclusion: Namespace does not exist in Javascript, but let's define we have it also in javascript Assuming this, I could use closures (wrapping variables, so that they can be accessed by a particular function) to realize the namespace. Whereas the namespace would be "particular Function).

Did I understand it correct now, assuming we have following Example from up above (plus a var):

var Interface = (function(){
    var someVar = "";
    function name(p1, p2) {
    }
    return {
        name: name,
       value: function() {
               return someVar;
     }
    }; 
})();
  • This is a closure, wich wraps "someVar" and "name".
  • The closure is assigned to an variable "Interface".
  • The "Interface" is the namespace. It can be "activated" by writing 'Interface.call()'

Correct, wrong, other meanings?

Thanks, Meru

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM