export class Http { m1(s:string) { console.log("in m1",s); } m2(s:string) { console.log("in m2",s); }}export class a{ http=new Http(); op(s:string) { console.log(this); this.http.m1(s+"from a"); }}export class b { http=new Http(); constructor() { } op1(s:string) { console.log(this); this.http.m2(s+"from b"); }}//main function calllet v = 2let ptr = null;let a1 = new a();let b1 = new b();switch(v) { case 1: ptr=a1.op; break; case 2: ptr=b1.op1; break;}ptr("s");在上面的例子中,我創建了一個&b類,分別有op &op1方法。根據我的選擇(如在 switch 語句中),我想調用一個方法。但我收到一個錯誤“無法讀取未定義的屬性'http'”。誰能解釋一下為什么會發生這種情況?
在類型腳本中。為什么從函數變量調用函數時未定義“this”關鍵字值?
滄海一幻覺
2022-09-29 15:27:53