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

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():但是它不起作用。

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


注意:请忽略错别字。

最佳答案

如果要通过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;
   }

}

关于javascript - 如何将函数中的变量用于同一类的其他块,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46770052/

10-12 12:30