简体   繁体   中英

Passing variables through parameters in Javascript

Here I have a block of code that I've been debugging over Firebug. I want to know how I can pass my parameter foo into the function getHoliday(param,param,param) . Here is the block of code from my XMLHttpRequest.

if (XMLHttpRequestObject.readyState==4 && XMLHttpRequestObject.status == 200)
{
    var pp = null
    var dd = null
    var xmlResponse = XMLHttpRequestObject.responseXML;
    var foo = new Array();
    foo = parseXML(xmlResponse);
    getHoliday(pp,dd,foo);
} 

The first two parameters of getHoliday are un-used until later in the process. I want to first load getHoliday with data from the array foo so in the future I can use foo as shown below. As soon as my break point arrives at getHoliday, the script stops so I believe it's the parameters that are wrong. I want to point out, pp and dd are nothing, just place holders for empty undefined parameter.

function getHoliday(monthSelected,theday,names)
{   
    var HolidayName = new Array();
    var holiday = ""
    HolidayName = names;  
    monthSelected = monthSelected + 1;
    for(var index = 0; HolidayName.length >= index; index++)
    {   
        if(HolidayName[index] == monthSelected && HolidayName[index+1] == theday)
        {
            holiday = HolidayName[index+2]
        }
    }
    return holiday
}

As soon as my gets down here, names array that I just passed becomes undefined. Why? Below is how HolidayName array should look.

HolidayName = new Array(2,4,"Party--12:00",2,22,"Eventalist Launch--6:00",2,18,"Play Day--12:00",3,17,"Play Day--12:00"););

When alerting foo the result is:

2,4,"Party--12:00",2,22,"Eventalist Launch--6:00",2,18,"Play Day--12:00",3,17,"Play Day--12:00"

Here is parseXML

function parseXML(xmlData)
{
    var aOutput=document.getElementById("testing2");
    var events = xmlData.getElementsByTagName('year').length;
    for(var i=0;i< events;i++)
    {
        var eYear = xmlData.getElementsByTagName('year')[i].firstChild.nodeValue;
        var eMonth = xmlData.getElementsByTagName('month')[i].firstChild.nodeValue;
        var eDay = xmlData.getElementsByTagName('day')[i].firstChild.nodeValue;
        var eHour = xmlData.getElementsByTagName('hours')[i].firstChild.nodeValue;
        var eMinute = xmlData.getElementsByTagName('minutes')[i].firstChild.nodeValue;
        var eTitle = xmlData.getElementsByTagName('title')[i].firstChild.nodeValue;
        var holiStr = '"' + eTitle + "--" + eHour  +":"+ eMinute + '"';
        setup.push(eMonth,eDay, holiStr);
    }
    return setup;
}

Given the information you have provided, best guess: do you have var foo lower in the code? Javascript does not have block level scoping and these get hoisted to the top of functions and var foo lower could alter your foo value.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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