2015-09-18 66 views
0

我在查看flux時發現了一些奇怪的typescript語法,這沒有任何意義。例如;可以重載TypeScript導出嗎?

class Action { 
    private _source: Action.Source 

    constructor(source: Action.Source) { 
     this._source = source 
    } 

    get source() { 
     return this._source 
    } 
} 

module Action { 
    export enum Source { 
     View, 
     Server 
    } 
} 

export = Action 

export = Action在這裏做什麼?是否重載導出模塊和類?以某種方式混合它們?我不理解這裏的語義..

回答

1

它使用聲明合併。什麼是幕後發生的事情基本上是這樣的:

// class is defined 
function Action(source) { 
    this._source = source; 
} 

Object.defineProperty(Action.prototype, "source", { 
    get: function() { 
     return this._source; 
    }, 
    enumerable: true, 
    configurable: true 
}); 

// enum is defined on the Source property of Action—NOT on Action's prototype 
Action.Source = ...enum object definition... 

export = Action; 

瞭解更多關於在Handbook「帶類,函數和枚舉合併模塊」。