JavaScript未定义 [英] JavaScript is getting undefined

查看:82
本文介绍了JavaScript未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨专家,



你能看到我的下面的代码,请告诉我为什么我从gup函数中得到了不确定。



Hi Experts,

Could you please see my below code and kindly tell me why I am getting undefined from gup function.

function BtnSelect_Click_Complete(values) {

            var ofiledname;
            //var oParticipantListID;

            //oParticipantListID = participantListID();

            //alert('aaa');
            ofieldname = gup('fieldname');           
            if (ofieldname != null) {
                //alert(ofieldname + " : " + values[0] + " : " + values[1]);
                window.opener.SetLookup(opener.document, ofieldname, values[0], values[1]);
                window.opener.PopulateParticipant(values[0], ofieldname);
            }
            self.close();
        }







function gup( name )
{
    var regexS = "[\\?&]"+name+"=([^&#]*)";
    var regex = new RegExp( regexS );
    var tmpURL = window.location.href;    
    var results = regex.exec( tmpURL );
    if( results == null )
        return "";
  else
    return results[1];
}





请帮帮我,我有生产问题。

提前致谢。



Shafik



Please help me, I have production issue.
Thanks in advance.

Shafik

推荐答案

根据文档, exec 返回数组或null,但不是未定义。因此,如果(结果== null)返回; 不会导致问题。让我们看看其他部分。如果结果少于两个成员,则可能会从函数中返回未定义的结果。如果你可以使用JavaScript调试器,你很容易找到它。



所以,至于第一点,if下的条件,我宁愿是在安全方面,所以我会写

According to the documentation, exec returns either array or null, but not undefined. So, if (results == null) return ""; could not cause a problem. Lets look at "else" part. If results has less then two members, you could have undefined return from your function. If you could use JavaScript debugger, you would easily find it out.

So, as to the first point, the condition under "if", I would prefer to be on the safe side, so I would write
if (results) { // not comparing with null, to be of safe side
   if (results.length > 1) // we don't know know if we have an element at [1]
      return results[1];
   else
      return ""; // no second element
} else
   return ""; // wouldn't null be better?



不是在两种情况下返回,而是可以返回不同的东西,以区分数组不足以在[1]处有元素的情况,以及根本没有匹配的情况。这就是为什么我建议在这种情况下返回null,在另一个中。或类似的东西。此外,您可以考虑在使用正则表达式之前检查 name 为null,然后清空字符串,以传递相同的值以返回。



-SA


Instead of returning "" in two cases, you could return something different, to distinguish the case when the array is not long enough to have an element at [1], and the case where there is no a match at all. That's why I suggested to return null in this case, "" in another one. Or something like that. Also, you could think of checking up name for null and then empty string before using Regex, to pass the same values to return.

—SA


这篇关于JavaScript未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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