jshint - 如何修复我的 if 语句

发布于 2024-12-15 07:51:42 字数 1274 浏览 0 评论 0原文

这段代码工作正常......尽管可能很难阅读。不过,我想遵守 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

长发绾君心 2024-12-22 07:51:42

这是关于我如何清理它的:

function interface_signin() {
    var form_elements = document.forms.signin.elements,
        response_div  = 'signin_response',
        client_fail   = false;

    if(arc.client_validation === 1) {
        client_fail = 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);
    }

    if(!client_fail) {
        ajax('arche_model.php', serialize(form_name) + '&a=signin', ajax_signin, response_div);
    }
}

This is about how I’d clean it up:

function interface_signin() {
    var form_elements = document.forms.signin.elements,
        response_div  = 'signin_response',
        client_fail   = false;

    if(arc.client_validation === 1) {
        client_fail = 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);
    }

    if(!client_fail) {
        ajax('arche_model.php', serialize(form_name) + '&a=signin', ajax_signin, response_div);
    }
}
究竟谁懂我的在乎 2024-12-22 07:51:42

您的第一个和最后一个条件是分配:

(client_fail = 1)
... && (client_fail = 0)

再次,boss:true 开关。

请使用更多的空白。

Your first and last conditions are assignments:

(client_fail = 1)
... && (client_fail = 0)

Again, boss:true switch.

Please use more whitespace.

断念 2024-12-22 07:51:42

为了使其更具可读性,我将操作行更改为:

client_fail = !(
       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 而是检查 !client_fail

To make it more readable, I'd change the operative line to:

client_fail = !(
       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));

instead of setting flags and unsetting them on success.
and instead of checking client_fail === 0 just check !client_fail.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文