如何将函数的varibale用于同一个类的其他块

时间:2017-10-16 12:12:16

标签: javascript typescript

如何在同一个类中使用某个函数的成员来检查同一类中某个其他块中的条件:

class SomeClass{

let whatToDo: string;

public result(){  // want to implement the code but not able to.The only condition is that I want to call it from the Final() function only.

        if ( // the value of latest is not equal to 'addition' ){
             return the value of latest
           }
        else if (// if the value of latest is not equals to 'multiplication'){
              return latest;

        else return;
}

}
public addition(){
    this.whatToDo = 'addition';
     this.Final(this.whatToDo);
      return;
}

public multiplication(){
     this.whatToDo = 'multiplication';
     this.Final(this.whatToDo);
      return;
}

private Final(type:string){

       latest = type;
 }
}

我已尝试按如下方式实现上述结果():但它不起作用。

result(){
 if(this.Final.latest != 'addition') {
   return this.Final.latest;
}
 else if (this.Final.latest != 'multiplication') {
    return this.Final.latest;
}
else return;
}

注意:请忽略错别字。

1 个答案:

答案 0 :(得分:2)

如果您想通过this进行访问,则需要将其设为该类的成员,并将其声明为whatToDo: string

也让你的latest成为班上的一员。

像这样。

class SomeClass {

   whatToDo: string;
   latest: string;

   public result() {  

      if (this.latest !== 'addition'){
           return the value of latest
      } else if (this.latest !== 'multiplication'){
            return latest;
      } else {
            return;
      }

   }

   public addition() {
       this.whatToDo = 'addition';
       this.Final(this.whatToDo);
   }

   public multiplication() {
       this.whatToDo = 'multiplication';
       this.Final(this.whatToDo);
   }

   private Final(type:string){ 
       this.latest = type;
   }

}