2017-04-10 57 views
0

現在JS有class,我想知道這3個「站」是否有區別。有相同的/相同的嗎? 在所有情況下,我可以station.label類,對象和構造函數是否等價?

//1 
export class Station { 

    public label: string; 
    public code: number; 

    constructor(label, code) { 
     this.code = code; 
     this.label = label; 
    } 
} 
let station = new Station("my label", "my code"); 

//2  
function Station(label, code) { 
    this.label = label; 
    this.code = code; 
} 
let station = new Station("my label", "my code"); 

// 3 
let station = { label: "my label", "code": my code } 

回答

0

訪問標籤可以說是這樣的:在任何情況下你得到的標籤&代碼屬性的新對象;)

到底類

被transpiled進入功能:

class A { 
    ... 
} 

被transpiled到像水木清華:

var A = function A() { 
    _classCallCheck(this, A); 
}; 

所以你得到一個等價的回報,但如何創建實例將有點不同;)

例如,
例1 & 2 - >new Station()
例3 - >Object.create(Station.prototype)