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

0x800a138f - JavaScript runtime error: Unable to get property 'replace' of undefined or null reference

本文关键字:replace 未定义 引用 属性 运行时错误 JavaScript 获取 0x800a138f      更新时间:2023-09-26
if(theCurrentLength == 0)
    {
        theCurrentStory++;
        theCurrentStory      = theCurrentStory % theItemCount;
        theStorySummary      = theSummaries[theCurrentStory].replace(/"/g,'"');
        theTargetLink        = theSiteLinks[theCurrentStory];
        theAnchorObject.href = theTargetLink;
        thePrefix            = theLeadString;
    }

上述属性有什么问题?

replace属性有什么问题?

没有问题。问题是

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存储在数组项中。唯一确定的方法是使用浏览器内置的调试器,并逐步检查代码,同时查看变量的值。