js----modules2(用原文是翻译词不达意)

用 IIEF 来创建简单的继承结构:

let Castle = (function () {
    function Castle(name) {
        this.name = name;
    }
    Castle.prototype.Build = function () {
        console.log("Castle built :"+ this.name);
    };
    return Castle;
})();
Westros.Structures.Caslte = Castle;

这种结构容易理解由于它层次自然,使用此结构的继承也相对容易完成。
现在我们定义一个BaseStructure 作为所有 structures 的父类,实现上面的结构。

let BaseStructure = (function () {
    function BaseStructure() {
    }
    return BaseStructure;
})();
Structures.BaseStructure = BaseStructure;
let Castle = (function (_super) {
    _extends(Castle, _super);
    function Castle(name) {
        this.name = name;
        _super.call(this);
    }
    Castle.prototype.Build = function () {
        console.log("Castle built: "+this.name);
    };
    return Castle;
})(BaseStructure);

You’ll note that the Basestructure is passed into the Castle object when the closure is evaluated.还有一个helper方法_extends(),负责把基类里面里面所有的functions复制到派生类里面。

let _extends = this._extends || function (d,b) {
    for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
    function _() {this.constructor = d;};
    _.prototype =b.prototype;
    d.prototype = new _();
}

We can continue the rather nifty closure syntax we’ve adopted for a class to implement an entire module

var Westeros;
(function (Westeros) {
    function (Structures) {
        let Castle = (function(){
            function(name){
                this.name = name;
            }
            Castle.prototype.Build = function(){
                console.log("Castle built:" + this.name);
            };
            return Castle;
        })();
        Structures.Castle = Castle;
    }(Westeros.Structures || (Westeros.Structures = {}));
    var Structres = Westeros.Structures;
})(Westeros || (Westeros = {}));

Within this structure you can see the same code for creating modules that we
explored earlier. It is also relatively easy to define multiple classes inside a single
module. This can be seen in this code

var Westeros;
(function (Westeros) {
    (function (Structures) {
        let Castle = (function(){
            function Castle(name){
                this.name = name;
            }
            Castle.prototype.Build = function(){
                console.log("Castle built:"+ this.name);
                var w = new Wall();
            };
            return Castle;
        })();
        Structures.Castle = Castle;
        var Wall = (function(){
            function Wall(){
                console.log("Wall constructed");
            }
            return Wall;
        })();
        Structures.Wall = Wall;
    })(Westeros.Structures || (Westeros.Structures = {}));
})(Westeros || (Westeros = {}));

Structures.Wall = Wall:
shows exposing the class outside of the
closure. If we want to make private classes that are only available within the module then we only need to exclude that line.
This is actually known as the revealing module pattern. We only reveal the classes that need to be globally available。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章