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'”。谁能解释一下为什么会发生这种情况?
添加回答
举报
0/150
提交
取消