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。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值