2

First off, all I'm trying to do is to structure my javascript code properly. I've been told I must not have anything global. So I took the 2 types of namespaces declaration from this answer and now asking you guys for the pros and cons of each.

    var namespace1 = new function () {
        var internalFunction = function () {
        };

        this.publicFunction = function () {
        };
    };

    var namespace2 = {
        publicFunction: function () {
        }
    };

Also, how do i have a private function in the last one namespace2?

5
  • I am frankly surprised this question does not have an answer yet. Commented Jun 26, 2011 at 23:18
  • 2
    Where is @Felix Kling or @BoltClock or @Pointy or @alex? One of those guys. I'd like to see what you guys have to say... ;) Commented Jun 26, 2011 at 23:42
  • @Jared: Seems we were too late :D Actually I'm surprised that the first syntax (namespace1) works. Didn't know that you can have an "immediate constructor" this way. Always learning... Commented Jun 26, 2011 at 23:56
  • "I've been told I must not have anything global." - I think, rather, say "I must minimise my use of globals", because your namesapce object needs to be global. But certainly keeping all of your code within your own namespace is a good plan. Commented Jun 27, 2011 at 0:49
  • Felix - ECMA 262 §11.2.2 defines NewExpression as new NewExpression, §11.2 says that NewExpression can be new MemberExpression and a function expression is a MemberExpression. Commented Jun 27, 2011 at 1:40

4 Answers 4

3

It depends how complex your object is. For most cases, the first method is bloated and less clear. A simple object declaration is usually much cleaner:

var namespace = {
    f1: function () {
        // Do thing 1
    },
    f2: function () {
        // Do thing 2
    }
};

However, it is not possible to create private functions like this, as only functions, not object literals, create scope. Depending on your school of thought, this may not be a problem. I like to have all my methods "public", but indicate that some of them are meant for internal use by prefixing their names with an _. This lets you easily unit test functions intended to be "private".

At the same time, you may have meaningless temporary variables (like i) used to initialise the namespace, which you do want to hide by using a function.

Of course, the first method of using a function gives you more control, not only over private/public access, but over when the "namespace" is initialized. The example you used instantiates the namespace immediately with new, but can also choose to do this on DOM ready, or after some AJAX has completed.

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

Comments

0

You can always wrap it in a closure, and return it with you explicit visibility.

namespace2 = (function(){
  function private_function() {
    //..
  }

  function publicFunction1() {
    private_function(); // accessible here
  }

  return {
    exportedPublicFunction1: publicFunction1
  };
})();

namespace2.exportedPublicFunction1();

Comments

0

private function in namespace2.

publicFunction reveal privateFunction using Revelation Pattern.

var namespace2;

(function() {

    function privateFunction() {
    }

    namespace2 = {
       publicFunction : privateFunction
    };

}());

namespace2.publicFunction();

Comments

0

Take a look at Javascript closures. It took me a while to get them but they will allow you to have private members. Here's an article by Douglas Crockford on them that's pretty straightforward, and another by the Mozilla folks.

I use closures and expose selected methods through namespaces. It works pretty well and helps us keep our code tidy.

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.