简体   繁体   English

未定义变量的Javascript错误

[英]Javascript error with undefined variable

I have a problem where a method is getting an undefined variable error, even though I check for the variable being undefined before calling. 我有一个问题,即即使我在调用之前检查了未定义的变量,方法也会收到未定义的变量错误。

// Sets focus and text-select to the passed in element.
idNav.prototype.setFocusFromVar = function(r) {
    document.activeInputArea = r;   // my variable for tracking focus
    r.focus();    // error happens here (line 274 of idNav.js)
    r.select();
}

The error happens at the r.focus line. 该错误发生在r.focus行。

In both places where I call the method, I use a pattern similar to the following with a local variable r in the caller. 在调用方法的两个地方,我都使用与以下类似的模式,并在调用方中使用局部变量r。

if (!r)
    return;

this.setFocusFromVar(r);

and yet the error persists. 但是错误仍然存​​在。 When r is not null or undefined, it is an input element in a table on my webpage. 当r不为null或未定义时,它是我网页上表中的输入元素。

I continue to get r is undefined on line 274 of idNav.js, which is the r.focus line. 我继续在idNav.js的第274行(即r.focus行)上未定义r。 All of the callers of the method are in the same js file as well. 该方法的所有调用者也都在同一个js文件中。

What am I missing? 我想念什么?

This error is occurring intermittently in Firefox, I haven't tested this specific error in IE. 此错误在Firefox中间歇性地发生,我尚未在IE中测试此特定错误。

EDTA: r did show up as an undefined and the stack trace of the error shows: EDTA:r确实显示为未定义,并且错误的堆栈跟踪显示:

setFocusFromVar()(undefined)IDNav.js (line 275)
dhandler(Object originalEvent=Event keydown type=keydown)IDNav.js (line 100)
newTrigger()(Object originalEvent=Event keydown type=keydown)jquery.hotkeys.js (line 1)
F()()jquery.js (line 19)
F()(Object originalEvent=Event keydown type=keydown)jquery.js (line 19)
F()()jquery.js (line 19)
[Break on this error] r.focus();

dhandler is one of the methods I checked out and seemed to be good (no problems). dhandler是我签出的方法之一,看起来不错(没问题)。 I will take another look at that to be sure though: 我将再次对此进行确认:

It is used for handling the down-arrow and enter keys for navigation through my table of input elements. 它用于处理向下箭头,并输入用于在我的输入元素表中导航的键。

Based on your description of the problem, it seems to me that it is sometimes getting called without the sanity check. 根据您对问题的描述,在我看来,有时不进行健全性检查就调用它。 I'd put the sanity check inside the function rather than outside of it. 我将健全性检查放在函数内部而不是外部。

However, you also probably want to know how you're getting around it in the first place. 但是,您可能首先也想知道如何解决它。 I'd modify the function as follows to inspect what is going wrong in Firebug: 我将如下修改函数,以检查Firebug出了什么问题:

idNav.prototype.setFocusFromVar = function(r) {
    if (!r) {
        return;  // Set the breakpoint here
    }
    document.activeInputArea = r;
    r.focus();
    r.select();
}

Then when you hit the breakpoint, you can look at Firebug's stack trace to determine how you got the function without checking whether r was defined or not. 然后,当您达到断点时,可以查看Firebug的堆栈跟踪来确定如何获得该函数,而无需检查是否已定义r。

I'd recommend moving (or duplicating) your sanity check inside the function as follows: 我建议按以下方式在功能内移动(或复制)您的健全性检查:

idNav.prototype.setFocusFromVar = function(r) {
    if (!r)
        return;

    document.activeInputArea = r;   // my variable for tracking focus
    r.focus();    // error happens here (line 274 of idNav.js)
    r.select();
}

I am not shure, but shouldn't you call if(r==undefined) instead of if(!r)? 我不是很确定,但是您不应该调用if(r == undefined)而不是if(!r)吗? I always do it like that... 我总是那样做...

You should test that the variable is not null and is not equal to undefined . 您应该测试该变量不为null,并且不等于undefined

Here's a good article on testing variables for existence in Javascript. 这是一篇关于测试变量是否存在于Javascript中的好文章 It lists various ways to do it as well as the pros and cons of each method. 它列出了执行此操作的各种方法以及每种方法的利弊。

I rarely use if(!r) to test if a variable is undefined. 我很少使用if(!r)来测试变量是否未定义。

I prefer using if(typeof(r)=='undefined') or if(r!=null). 我更喜欢使用if(typeof(r)=='undefined')或if(r!= null)。

the value of r depends of its declaration and affectation. r的值取决于它的声明和影响。

  • var r; var r; =>undefined but testable to null (r==null will return true) =>未定义,但可测试为null(r == null将返回true)
  • var r="test"; var r =“ test”; => string test =>字符串测试
  • var r=null; var r = null; => null; => null;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM