11

For my web application, I am creating a namespace in JavaScript like so:

var com = {example: {}};
com.example.func1 = function(args) { ... }
com.example.func2 = function(args) { ... }
com.example.func3 = function(args) { ... }

I also want to create "private" (I know this doesn't exist in JS) namespace variables but am not sure what's the best design pattern to use.

Would it be:

com.example._var1 = null;

Or would the design pattern be something else?

5
  • 2
    By "great" do you mean "create"? Commented Nov 10, 2010 at 20:22
  • possible duplicate of JavaScript Namespace Declaration Commented Nov 10, 2010 at 20:27
  • @casablanca, no - my question is not a duplicate of what you linked too. Commented Nov 10, 2010 at 20:30
  • @Staceyl: The question I linked to has an example of how to create private variables within a namespace. Commented Nov 10, 2010 at 20:32
  • @Staceyl: first hit on google, searching for "javascript private member": crockford.com/javascript/private.html Commented Nov 10, 2010 at 20:33

3 Answers 3

9

Douglas Crockford popularized so called Module Pattern where you can create objects with "private" variables:

myModule = function () {

        //"private" variable:
        var myPrivateVar = "I can be accessed only from within myModule."

        return  {
                myPublicProperty: "I'm accessible as myModule.myPublicProperty"
                }
        };

}(); // the parens here cause the anonymous function to execute and return

But as you said Javascript doesn't truly have private variables and I think this is somewhat of a cludge, which break other things. Just try to inherit from that class, for example.

Sign up to request clarification or add additional context in comments.

Comments

8

Closures are frequently used like this to simulate private variables:

var com = {
    example: (function() {
        var that = {};

        // This variable will be captured in the closure and
        // inaccessible from outside, but will be accessible
        // from all closures defined in this one.
        var privateVar1;

        that.func1 = function(args) { ... };
        that.func2 = function(args) { ... } ;

        return that;
    })()
};

1 Comment

To add to this answer: This article describes a few different variations of the module pattern that you should consider. adequatelygood.com/2010/3/JavaScript-Module-Pattern-In-Depth
0

After 7 years this might come quite late, but I think this might be useful for other programmers with a similar problem.

A few days ago I came up with the following function:

{
    let id    = 0;                          // declaring with let, so that id is not available from outside of this scope
    var getId = function () {               // declaring its accessor method as var so it is actually available from outside this scope
        id++;
        console.log('Returning ID: ', id);
        return id;
    }
}

This might only be useful if you are in a global scope and want to declare a variable that is not accessible from anywhere except your function that sets the value of id one up and returns its value.

Comments

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.