gpt4 book ai didi

javascript - 如何将函数中的变量使用到同一类的其他 block 中

转载 作者:行者123 更新时间:2023-12-01 02:54:45 25 4
gpt4 key购买 nike

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

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

同时让您的最新也成为类(class)成员。

类似这样的事情。

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 - 如何将函数中的变量使用到同一类的其他 block 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46770052/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com