问题描述:

I am trying to alert the values in each conditional of a loop below (in the function returnFunction). I have a function myFunction that is structured as so:

var myFunction = function () {

window.myArray = {

1: returnFunction("a"),

2: returnFunction("b"),

3: returnFunction("c"),

4: returnFunction("d"),

5: returnFunction("e"),

6: returnFunction("f"),

7: returnFunction("g")

}

}

returnFunction looks like this:

var returnFunction = function (classID) {

var myArray = [], tempcounter = 0;

tempcounter = document.getElementsByClassName(classID).length;

myArray = {classID : tempcounter};

return alert(myArray[0] + ' ' + myArray[1]);

}

The returned value that is alerted is undefined undefined

It's my understanding the flow here is that when myFunction is called, it calls returnFunction 7 times, each time sending a different string ("a" through "g") that represent a specific class in the DOM. For a given class "a" through "g", the number of instances of the class (classID) is then counted and returns the classID and a number, which should be returned in the alert.

Can anyone tell me where I am going wrong here?

网友答案:

This code is something of a mess, so I'll try to clean it up.

Assuming the point of this function is to capture the current length for all objects on the page, this is a bit clearer:

var classArr = ['a','b','c','d','e','f','g'],
    lengthObj = {},
    getLength = function(classID){
        return document.getElementsByClassname(classID).length;
    },
    lengthFunc = function(){
        for(var i = classArr.length; i--;){
            lengthObj[i+1] = {
                classArr[i]:getLength(classArr[i])
            };
        }
    }

lengthFunc();

This does the following:

  • Builds an array of the classIDs you have
  • Creates window-level empty object
  • Creates method to return the number of items for a specific classID
  • Creates method that assigns each length retrieved from getLength method to its appropriate classID

By the way, it made no sense whatsoever to return an alert to a value of an object (or array, either one), but it made way more sense to assign the object of classID:length to each value. Last note is that I would make the lengthFunc 0-indexed instead of 1-indexed, but I kept it the same as you had it, hence the [i+1].

网友答案:

In the returnFunction, change

myArray = { classID : tempcounter};
return alert(myArray[0] + ' ' + myArray[1]);

to

myArray[classID]  = tempcounter;
return alert(classID + ' ' + myArray[classID]);
网友答案:

You wrote

myArray = { classID: tempCounter}

In particular you created an object, not an array, with one key, the key being classID. This object does not have the key 0 nor the key 1. Therefore myArray['classID'] will be defined, but myArray[0] will not be. Note the object

foo = { '0': tempCounter }

will be defined for foo[0]. This is what it would like to create an object with keys as numbers, in contrast to creating an array.

You should use an array instead of an object, or access the object as objects are accessed, not as arrays are accessed.

相关阅读:
Top