Javascript 如何将变量从一个函数转换到同一类的其他块中
如何使用同一类中某个函数的成员检查同一类中某个其他块的条件:Javascript 如何将变量从一个函数转换到同一类的其他块中,javascript,typescript,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
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;
}
注意:请忽略输入错误。如果您想通过
此
访问,您需要将其设置为类的成员,并仅声明它whatToDo:string
也要让你的成为班上最新的
像这样的
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;
}
}
如果您想通过此
访问,则需要将其设置为类的成员,并将其声明为whatToDo:string
也要让你的成为班上最新的
像这样的
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;
}
}
我不能直接使用result()
函数定义中的Final函数调用并检查Final()
返回的结果吗?如果我在Final()
函数定义中添加returnthis.latest
,则提供。您认为它必须返回什么?您需要在哪里保存最新的值?我认为您需要调用一个函数来存储最后一个键,并使用最后一个键成员进行检查。你的函数只需要保留最后一个键的逻辑明白了吗,我只能使用该函数的成员变量?正确吗?不是函数,而是类成员变量。您也可以将其设置为私有。我是否可以直接使用result()
函数定义中的最终函数调用并检查Final()
返回的结果?如果我在Final()
函数定义中添加returnthis.latest
,则提供。您认为它必须返回什么?您需要在哪里保存最新的值?我认为您需要调用一个函数来存储最后一个键,并使用最后一个键成员进行检查。你的函数只需要保留最后一个键的逻辑明白了吗,我只能使用该函数的成员变量?正确吗?不是函数,而是类成员变量。你也可以把它保密。