samedi 31 décembre 2016

Javascript Modules and Design Patterns

I have some questions about the way you can create modules in JS. 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 form 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 fro this? Maybe to "reveal" the class?

Thx!

Aucun commentaire:

Enregistrer un commentaire