2017-04-10 5 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ます関数に:

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

class A { 
    ... 
} 

は次のようになめらかにtranspiledます

だから同等のリターンが得られますが、インスタンスの作成方法は少し異なります;)


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

関連する問題