3

I have some questions about the way you can create modules in JavaScript. This is the syntax I found:

var Westeros;
(function (Westeros) {
    (function (Structures) {
        var Castle = (function () {
            function Castle(name) {
                this.name = name;
            }
            Castle.prototype.build = function () {
                console.log("Castle " + this.name)
            };
            return Castle;
        })();
        Structures.Castle = Castle;
    })(Westeros.Structures || (Westeros.Structures = {}));
    var Structures = Westeros.Structures;
})(Westeros || (Westeros = {}));

var winterfell = new Westeros.Structures.Castle("Winterfell");
winterfell.build();

I took this code from Mastering Javascript Design Patterns. However I've tried to find an answer why you need this line:

    var Structures = Westeros.Structures;

If you omit this line the code works as expected. Is there any explanation for this? Maybe to "reveal" the class?

Thanks!

8
  • 7
    Damn, that's hideous code. There's a book that tells you to do that? Commented Dec 31, 2016 at 16:21
  • The line in question is completely pointless. Perhaps it is there because some subsequent example introduces more code, but in the code posted here the line does nothing. Commented Dec 31, 2016 at 16:28
  • Definitely hard to read... Commented Dec 31, 2016 at 16:30
  • It is indeed hard to read right? Any suggestions to modularize JS code? Not ECMA6 Commented Dec 31, 2016 at 16:48
  • I use require.js, it's for dependency management and modularized code Commented Dec 31, 2016 at 17:07

1 Answer 1

1

This code is horrifying. Here's a sample that does the exact some thing without all the pointless closures:

function Castle(name) {
    this.name = name;
}

Castle.prototype.build = function() {
    console.log("Castle " + this.name)
};

var Westeros = {
    Structures: {}
};

Westeros.Structures.Castle = Castle;

var winterfell = new Westeros.Structures.Castle("Winterfell");
winterfell.build();

When defining a "class" in ES5, all you have to do is declare a function with some arguments and place whatever initialization code you want inside. Methods are added to that function's .prototype property, and new objects using that class are created by calling its constructor function with the new keyword.

As for var Structures = Westeros.Structures;, it serves no purpose whatsoever. If you were to keep your original code sample, removing it wouldn't change anything. In fact, it can't be used even by more code further down because it's declared inside a closure and can't be accessed outside of that scope.

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

4 Comments

What about if you wanna simulate namespaces? A pretty straightforward solution would be just to create a global object and then start adding objects inside of it, isn't it?
That's correct. If you place this code in the global scope, Westeros will be a global variable and thus Castle can be accessed anywhere through the Westeros "namespace". You could also take a look and see if RequireJS fits your needs.
Awesome thanks! So, in vanilla javascript I've seen from different sources the creation of this type of modules (function(){})(). I assume with other solutions you can implement design patterns in similar ways.
@sms343 IIFEs ((function() { … })();) can be used with more or less any solution. Their primary purpose is to avoid polluting their parent scope, since all the function's declarations are lexically bound therein. I can edit my answer to use an IIFE if you want.

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.