Javascript 如何在angular9中重新分配变量?
我有一个函数,它调用Javascript 如何在angular9中重新分配变量?,javascript,angular,typescript,Javascript,Angular,Typescript,我有一个函数,它调用ngDoCheck()。该func调用另几个func,这些func重新分配了我的变量。但变量更改只在funcs中进行,而不是全局变量我的变量始终为0 myVariable = 0; ngDoCheck(): any { const word: string = this.form.get('word').value; this.PasswordStrengthMeter(word, this.myVariable); console.log('Val
ngDoCheck()
。该func调用另几个func,这些func重新分配了我的变量。但变量更改只在funcs中进行,而不是全局变量<代码>我的变量始终为0
myVariable = 0;
ngDoCheck(): any {
const word: string = this.form.get('word').value;
this.PasswordStrengthMeter(word, this.myVariable);
console.log('Value: ' + this.myVariable);
}
Mainfunc(word: string, myVariable: number): any {
this.SecondaryFunc(word, myVariable);
this.AnotherSecondaryFunc(word, myVariable);
}
如果它在同一个组件中,为什么要将该变量传递给每个函数?只需在该函数中使用它,因为它是一个全局变量。如果它在同一个组件中,为什么要将该变量传递给每个函数?只需在该函数中使用它,因为它是一个全局变量。myVariable的值将传递给
Mainfunc
,而不是引用
如果要更改全局变量,请直接从其他SecondaryFunc
和AnotherSecondaryFunc
中使用this.myVariable
myVariable = 0;
...
Mainfunc(word: string): any {
this.SecondaryFunc(word);
this.AnotherSecondaryFunc(word);
}
SecondaryFunc(word: string): any {
this.myVariable = 5;
}
AnotherSecondaryFunc(word: string): any {
this.myVariable = 6;
}
myVariable
的值正在传递给Mainfunc
,而不是引用
如果要更改全局变量,请直接从其他SecondaryFunc
和AnotherSecondaryFunc
中使用this.myVariable
myVariable = 0;
...
Mainfunc(word: string): any {
this.SecondaryFunc(word);
this.AnotherSecondaryFunc(word);
}
SecondaryFunc(word: string): any {
this.myVariable = 5;
}
AnotherSecondaryFunc(word: string): any {
this.myVariable = 6;
}
返回新值并在调用者中分配给
this.myVariable=this.SecondaryFunc
number属性按值传递,而不是按引用传递。返回新值并在调用者中分配给this.myVariable=this.SecondaryFunc
number属性按值传递,而不是按引用传递。