下面的打字稿函數在我打算使用的類中,而[col] =的賦值是我想進一步理解的,這是否意味着我可以訪問col父類還是另一種將它返回給調用者的方法?typescript handler where this [] = value
export class DemoResponsiveRows implements OnDestroy {
private _activeMQC : MediaChange;
private _watcher : Subscription;
firstCol = "row";
firstColXs = 'column';
firstColMd = 'column';
firstColLg = 'invalid';
firstColGtLg = "column";
secondCol = "column";
isVisible = true;
constructor(@Inject(MatchMediaObservable) private _media$) {
this._watcher = this._media$
.subscribe((e:MediaChange) => {
this._activeMQC = e;
});
}
ngOnDestroy() {
this._watcher.unsubscribe();
}
toggleLayoutFor(col) {
switch(col) {
case 1:
col = `firstCol${this._activeMQC ? this._activeMQC.suffix : ""}`;
this[col] = (this[col] === "column") ? "row" : "column";
break;
case 2:
col = "secondCol";
this[col] = (this[col] == "row") ? "column" : "row";
break;
}}}
改完成,我試圖瞭解 – gmaon
由於更新問題類的例子,我們現在可以看到'_activeMQC'將會是未定義的或將具有「Xs」,「Md」,「Lg」或「GtLg」的屬性。由於後綴值可以在運行時動態更改,因此您要求的代碼在運行時計算「col」的值爲「firstCol」,「firstColXs」,「firstColMd」,「firstColLg」或「firstColGtLg」。然後,如果它是「行」,則將該變量的值切換到「列」,反之亦然。在「firstColLg」的情況下,其值是原始的「無效」,它會將其設置爲「列」。 – GPicazo