2

If I have a module with lots of stuff like this:

My.Module = (function () {
    function method1 () {}
    function method2 () {}
    ...
    function method99 () {}

    return {
        method1: method1,
        method2: method2,
        ...
        method99: method99
    };
})();

How could I avoid repeating the method names? They all appear in three places.

If I were to use a local variable like this:

My.Module = (function () {
    var exp = {};

    exp.method1 = function () {};
    exp.method2 = function () {};
    ...
    exp.method99 = function () {};

    return exp;
})();

What should the exp variable be called? exp, exports, exported, obj, object? Does it matter? What's the convention?

If there's a way that is better than both of these, what is it?

dodov
  • 5,206
  • 3
  • 34
  • 65
  • 2
    the name does not matter, it is just the reference to the object returning. – Nina Scholz Feb 17 '17 at 08:43
  • @NinaScholz I understand that the name doesn't affect the code in any way, but is there a convention for it? Is there a widely accepted name I can use which would immediately tell the reader "oh yeah, that's the public stuff"? – dodov Feb 17 '17 at 09:00
  • 1
    just call it what it keeps. – Nina Scholz Feb 17 '17 at 09:01
  • how are the methods generated? does the name reflects some content? – Nina Scholz Feb 17 '17 at 13:41
  • Nope. I ended up calling it `reveal` like `reveal.someMethod = ...` and then `return reveal`. Since it's _revealing_ module pattern, I guess that's a pretty suitable name. – dodov Feb 17 '17 at 15:06

1 Answers1

0

The first method is the cleanest way IMO.

As for the var name in your secnd method, name, it whatever you like the name won't be exposed. Module should be good.

But, if you really do not want to repeat the function names, you can do :

My.Module = (function () {

    return {
        method1: function method1 () {},
        ...
        method99: function method99 () {}
    };
})();
A.Perrot
  • 323
  • 1
  • 8