Vim中的语法声明函数可能无法返回

Syntastic in Vim claims function might fail to return

本文关键字:返回 函数 语法 声明 Vim      更新时间:2023-09-26

EDIT: Syntastic被配置为使用JSLint

编辑2:我使用JavascriptLint,而不是JSLint

在下面的代码中,语法警告function inside does not always return a value

function(){
 switch(age){
 case 1:
  return 'won';
 case 2:
  return 'too';
 case 3:
  return 'tree';
 default:
  throw 'wow man, you are old!';
  break; //technically redundant
 }
}

我意识到default不返回值,但throw结束执行。这是一个语法错误,还是我应该改变我的编码风格?我应该在throw之后放一个return语句吗?

Syntactic在底层使用JSLint检查Javascript语法。

众所周知,JSLint为了避免错误而过于谨慎。您可以忽略该消息以做出更好的判断,或者关闭此特定警告,或者在那里添加冗余的return

我个人更喜欢满足JSLint的要求而不是关闭警告。只要确保在return附近添加//Satisfying JSlint注释,这样其他人就会理解冗余。