关于条件表达式的样式或错误的 JSLINT 警告
我正在尝试修复 JSLINT 警告的两件事,但我不知道如何修复。哪一行应该替换为什么代码?我已在下面粘贴了有问题的代码和有关它们的警告。预先非常感谢您的建议/答复。
JSLINT 说:
第 137 行出现问题:需要条件表达式,但看到的是赋值。
while(elem = document.getElementById("optionsDiv"+g))
第 140 行出现问题:预期为“{”,但看到的是“return”。
return g;
function isAnyOptionVisible()
{
var g=0;
while(elem = document.getElementById("optionsDiv"+g))
{
if(elem.className==="optionsDivVisible")
return g;
g++;
}
return -1;
}
I'm trying to fix what JSLINT is warning about two things tha I don't know how to fix. Which line should be replaced with what code? I have pasted the code in question and the warnings about them below. Thanks very much in advance for your suggestion / answer.
JSLINT says:
Problem at line 137: Expected a conditional expression and instead saw an assignment.
while(elem = document.getElementById("optionsDiv"+g))
Problem at line 140: Expected '{' and instead saw 'return'.
return g;
function isAnyOptionVisible()
{
var g=0;
while(elem = document.getElementById("optionsDiv"+g))
{
if(elem.className==="optionsDivVisible")
return g;
g++;
}
return -1;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
条件没有任何问题,除了
elem
如果未在外部作用域中声明,则可能是全局的。如果未声明,请事先声明:如果您想让 JSLint 满意,您可以添加与 null 的显式比较:
我没有看到任何有关“Expected {”的内容。然而,将 g++ 与大括号放在同一行是奇怪的风格。
请记住,JSlint 部分取决于 Crockford 的个人偏好,您不必总是同意。
编辑:好的,第二个错误是因为 JSLint 希望您将返回值放在大括号中,例如:
这也是我更喜欢编码的样式。我确实发现它避免了某些错误。然而,这又是主观的。明显的权衡是它增加了两条线。
There is nothing wrong with the conditional, except that
elem
may be a global if it isn't declared in an outer scope. If it's not declared, do so before hand:If you want to make JSLint happy, you can add an explicit comparison to null:
I don't see anything about "Expected {". However, putting the g++ on the same line as the brace is odd style.
Remember that JSlint is partly about Crockford's personal preferences, which you don't always have to agree with.
EDIT: Okay, the second error is because JSLint wants you to put the return in braces, e.g.:
This is also the style I prefer to code in. I do find that it avoids certain errors. However, this is again subjective. The obvious tradeoff is that it adds two lines.