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?

Community
  • 1
  • 1
Shawn Mclean
  • 56,733
  • 95
  • 279
  • 406
  • I am frankly surprised this question does not have an answer yet. – Jared Farrish Jun 26 '11 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... ;) – Jared Farrish Jun 26 '11 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... – Felix Kling Jun 26 '11 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. – nnnnnn Jun 27 '11 at 00: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*. – RobG Jun 27 '11 at 01:40

4 Answers4

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.

David Tang
  • 92,262
  • 30
  • 167
  • 149
0

private function in namespace2.

publicFunction reveal privateFunction using Revelation Pattern.

var namespace2;

(function() {

    function privateFunction() {
    }

    namespace2 = {
       publicFunction : privateFunction
    };

}());

namespace2.publicFunction();
user278064
  • 9,982
  • 1
  • 33
  • 46
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();
mhitza
  • 5,709
  • 2
  • 29
  • 52
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.

Paul
  • 19,704
  • 14
  • 78
  • 96