单个等于if。的JavaScript。有什么好理由吗? [英] a single equals in an if. Javascript. Any good reason?

查看:87
本文介绍了单个等于if。的JavaScript。有什么好理由吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

jQuery.each(player, function(key, val){                     
     if (el = $("#pr_attr_plain_"+key)){
          el.text(val === "" ? 0 : " " + val);
     }
});

我继承了一个项目,我遇到了一些奇怪的事情。开始这个项目的人是一名经验丰富的程序员,当然比我自己更多。这样做是否有任何价值或原因(无论多么糟糕):

I inherited a project and I came across something strange. The guy who started the project is an expereinced programmer, definitely more so then myself. Is there any value or reason (no matter how bad) in doing this:

if (el = $("#pr_attr_plain_"+key))

它现在有效,它在我没有的部分代码中触摸。我不想改变它,并且在不知道它可能做什么的情况下产生意想不到的后果。

It works now and it's in a portion of the code that I don't have to touch. I don't want to change it and have it produce unexpected consequences without knowing what it might do.

推荐答案

它可以是正确的。
代码相当于:

It can be correct. The code is equivalent to:

jQuery.each(player, function(key, val){                     
     el = $("#pr_attr_plain_"+key);
     if (el){
          el.text(val === "" ? 0 : " " + val);
     }
});

这篇关于单个等于if。的JavaScript。有什么好理由吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆