猿问

类中扩展类的使用方法

我正在尝试在扩展类中使用扩展类的方法(这可能不是很清楚,但是请看下面的示例,您将会理解)。


我的模块是用打字稿制作的:


export default class Test1 {

  private storedCond: string;


  public cond = (cond: string) => {

    this.storedCond = cond;

    return this.storedCond.length;

  };

  public getStoredCond = () => this.storedCond;

}

然后我想使用的是js文件中的某些方式:


import Test1 from './modules/Test1';


class Test2 extends Test1 {

  // this line is not working, how can i make it work ?

  this.cond('a futur dynamic string');


  final = () => `${this.getStoredCond()} is ${this.getStoredCond().length} length`;

}


const test = new Test2();


console.log(test.final());

我的Test1模块有点像商店,我可以做这样的事情:sotredCond = this.cond('a futur dynamic string');在我的Test2课上,但这不是我想要做的。我想cond在我的Test2课程中提供一个字符串()并将其存储在我的Test1模块中。


慕娘9325324
浏览 151回答 2
2回答

守着星空守着你

我的Test2类的最终代码是:class Test2 extends Test1 {  constructor() {    super();    this.cond('a futur dynamic string');  }  final = () =>    `${this.getStoredCond()} is ${this.getStoredCond().length} length`;}
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答