7

Let's say I have the namespace,

var Namespace = {
    A : function() {
        alert('Hello!');
    },
    B : function() {
        // Call A() from here, do other stuff
    }
}

In this namespace, I intend for A to be a helper function to B. That is to say, A() will never be called outside the namespace. It will only be called by the functions within the namespace.

What's the best way to address the issue of a local/helper function within a namespace? The way I see it there are two possibilities:

// Method #1    
var Namespace = {
    A: function() {
        alert('Method #1');   
    },

    B : function() {
        Namespace.A();
    }
}

Namespace.B();

// Method #2
function Namespace2() {
    var A = function() {
        alert('Method #2');
    };

    this.B = function() {
        A();        
    }
}

var ns2 = new Namespace2();
ns2.B();

In the first method, it is ugly and awkard to type Namespace.A() (repeatedly) in every function within the namespace. This leads me to prefer Method #2. But I was curious what was the best practice here.

0

5 Answers 5

13

I recommend placing the "namespace" inside a function scope. Everything not explicitly public will be naturally private:

var Namespace = (function() {
    var self = {};

    // Private
    var A = function() {
        ...
    };

    // Public
    self.B = function() {
        A();
    }

    return self;
}());

Namespace.B(); // Works
Namespace.A(); // Doesn't work
Sign up to request clarification or add additional context in comments.

6 Comments

This is a nice solution. My question is, does Namespace redefine its functions every time you access it? If so, it would be a bad use of memory.
@ktm5124: No. The function is run immediately and the object self is assigned to Namespace.
@Codemonkey in your case you cannot make multiple Namespace objects.
@Neal: I don't follow. Why would you make instances of a namespace?
This is the most underrated post I have come across.
|
6

You can call it using this statement

this.A();

3 Comments

This is not guaranteed to call Namespace.A.
@Hubro if i write namespace instead of Namespace, it works for me.
this will depend on the context; if you are inside an event handler then this will not point to the top-level namespace so Namespace.A is more predictable inside nested scopes.
2

Well you can event use a third option where the Namespace is created in it's own scope:

var Namespace = (function(){
    var A = function() {
        alert('scoped method');
    };

    function Namespace() {

          var A1 = function() {
               alert('Namespace "private" method');
          };

          Namespace.prototype.B1 = function(){
              A();  //will run
              A1(); //will run with no errors
          };

    };

    Namespace.prototype.B = function(){
        A();  //will run
        A1(); //ERROR!
    };

    return Namespace;
})();

2 Comments

Thanks Neal, though I have the same question that I asked CodeMonkey in his response. That is, does Namespace redefine its functions every time you access it?
@ktm5124 in my case it does not. All you have to do is new Namespace everything else is already defined. You can technically do Namspace.__proto__.B(); and it will still run without the new part.
1

If you only intend to use A inside B, why not define it inside B?

var Namespace = {
    B: function() {
        var A = function() {
            ...
        }

        A();
    }
};

Namespace.B();

1 Comment

I actually intend to use A inside B, C, D, E, ... that is, all of the public namespace functions, which are many. (I have omitted them and only included B.)
-2
var Namespace = {
    A : function() {
        alert('Hello!');
    },
    B : function() {
        Namespace.A();
    }, 
}

note the Semi-colon at the end

3 Comments

Could you please add more details about the solution you provide?
The comma at the end? What does the comma (},) after B's function have to do with anything?
sorry, my english is not good. I want say it is Semi-colon ( , ). I note Semi-colon used to reply comma ( ; ) in snamepace <== GOOGLE STRANSLATE . I still code same that way and it is working .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.