jshint - 如何修复我的 if 语句
这段代码工作正常......尽管可能很难阅读。不过,我想遵守 jshint.com,也许可以使其更具可读性。
有很多不同的方法可以改变这个我不知道最好的方法是什么?
基本上我需要能够打开/关闭 client_validatin 。如果数据有效,我就会向服务器发出 ajax 调用。
当我编写它时,这感觉就像一个黑客,特别是 client_fail 的使用(即用于确定客户端是否通过验证。
对于此代码
function interface_signin()
{
var form_name='signin',
form_elements=document.forms[form_name].elements,
response_div='signin_response',
client_fail=0;
if(arc.client_validation===1)
{
(client_fail=1)&&check_empty(form_elements,response_div,messages.empty)&&check_item(patterns.email,form_elements[0],response_div,messages.email)&&check_item(patterns.pass,form_elements[1],response_div,messages.validate)&&(client_fail=0);
}
if(client_fail===0){ajax('arche_model.php',serialize(form_name)+'&a=signin',ajax_signin,response_div);}
}
我收到错误
Line 466: (client_fail=1)&&check_empty(form_elements,response_div,messages.empty)&&check_item(patterns.email,form_elements[0],response_div,messages.email)&&check_item(patterns.pass,form_elements[1],response_div,messages.validate)&&(client_fail=0);
预期赋值或函数调用,而是看到一个表达式。
This code works fine...though it may be difficult to read. However I want to conform to jshint.com, and perhaps make it more readable.
There are so many different ways to change this I don't know the best way?
Basically I need to be able to turn client_validatin on/off. And if data validates than I make the ajax call to the server.
This felt like a hack when I wrote it, particularly the use of client_fail (i.e. used to determine if the client passed validation.
For this code
function interface_signin()
{
var form_name='signin',
form_elements=document.forms[form_name].elements,
response_div='signin_response',
client_fail=0;
if(arc.client_validation===1)
{
(client_fail=1)&&check_empty(form_elements,response_div,messages.empty)&&check_item(patterns.email,form_elements[0],response_div,messages.email)&&check_item(patterns.pass,form_elements[1],response_div,messages.validate)&&(client_fail=0);
}
if(client_fail===0){ajax('arche_model.php',serialize(form_name)+'&a=signin',ajax_signin,response_div);}
}
I get Errors
Line 466: (client_fail=1)&&check_empty(form_elements,response_div,messages.empty)&&check_item(patterns.email,form_elements[0],response_div,messages.email)&&check_item(patterns.pass,form_elements[1],response_div,messages.validate)&&(client_fail=0);
Expected an assignment or function call and instead saw an expression.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是关于我如何清理它的:
This is about how I’d clean it up:
您的第一个和最后一个条件是分配:
再次,
boss:true
开关。请使用更多的空白。
Your first and last conditions are assignments:
Again,
boss:true
switch.Please use more whitespace.
为了使其更具可读性,我将操作行更改为:
而不是设置标志并在成功时取消设置它们。
而不是检查
client_fail === 0
而是检查!client_fail
。To make it more readable, I'd change the operative line to:
instead of setting flags and unsetting them on success.
and instead of checking
client_fail === 0
just check!client_fail
.