javascript - javascript对'No Shadowed Variable'tslint警告的理解


private getNextStageStep(currentDisciplineSelected) {


 const nextStageStep = '';


 if (this.stageForDiscipline(this.currentDisciplineSelected) === 'step 1') {


 const nextStageStep = 'step 2';


 } else if (this.stageForDiscipline(this.currentDisciplineSelected) === 'step 2') {


 const nextStageStep = 'step 3';


 } else if (this.stageForDiscipline(this.currentDisciplineSelected) === 'step 3') {


 const nextStageStep = 'step 4';


 } else if (this.stageForDiscipline(this.currentDisciplineSelected) === 'step 4') {


 const nextStageStep = 'step 5';


 } else if (this.stageForDiscipline(this.currentDisciplineSelected) === 'step 5') {


 const nextStageStep = 'step 6';


 }


 return nextStageStep;


}



我要返回"nextstagestep"的值。

现在,tslint将每个"nextStageStep"变量强调为"no shadowed variables”。

时间:

linter抱怨的原因是,你多次重定义了相同的变量,


private getNextStageStep(currentDisciplineSelected) {


 let nextStageStep = '';


 if (this.stageForDiscipline(this.currentDisciplineSelected) === 'step 1') {


 nextStageStep = 'step 2';


 } else if (this.stageForDiscipline(this.currentDisciplineSelected) === 'step 2') {


 nextStageStep = 'step 3';


 } else if (this.stageForDiscipline(this.currentDisciplineSelected) === 'step 3') {


 nextStageStep = 'step 4';


 } else if (this.stageForDiscipline(this.currentDisciplineSelected) === 'step 4') {


 nextStageStep = 'step 5';


 } else if (this.stageForDiscipline(this.currentDisciplineSelected) === 'step 5') {


 nextStageStep = 'step 6';


 }


 return nextStageStep;


}



如果block中的变量为const nextStageStep,则声明为相同的变量。

替换 const nextStageStep = 'step 2';nextStageStep = 'step 2';

...