0x800a138f - JavaScript运行时错误:无法获取属性'替换'未定义或空引用

时间:2014-06-05 10:01:38

标签: javascript

if(theCurrentLength == 0)
    {
        theCurrentStory++;
        theCurrentStory      = theCurrentStory % theItemCount;
        theStorySummary      = theSummaries[theCurrentStory].replace(/"/g,'"');
        theTargetLink        = theSiteLinks[theCurrentStory];
        theAnchorObject.href = theTargetLink;
        thePrefix            = theLeadString;
    }

上述属性有什么问题?

1 个答案:

答案 0 :(得分:3)

  

替换poperty有什么问题?

没有问题。问题是

theSummaries[theCurrentStory]

...返回undefined(或null,但可能undefined)。

这表明,如果theSummaries是一个数组,theItemCount不等于theSummaries.length,那么您最终将theCurrentStory作为无效索引。当您索引具有无效索引的数组时,您将返回undefined。您可以直接使用theSummaries.length

if(theCurrentLength == 0) // <== Does that really make sense?
{
    theCurrentStory      = (theCurrentStory + 1) % theSummaries.length;
    theStorySummary      = theSummaries[theCurrentStory].replace(/&quot;/g,'"');
    theTargetLink        = theSiteLinks[theCurrentStory];
    theAnchorObject.href = theTargetLink;
    thePrefix            = theLeadString;
}

或者,如果索引有效,则可以将undefined存储在数组条目中。唯一可以确定的方法是使用浏览器内置的调试器并逐步执行代码,同时查看变量的值。