-2

我想知道为什么我pageCounter的页面对象(见下文)被视为字符串而不是 int?为什么javascript不解释变量,而是使用变量名作为文字字符串?

for (var i in stories){
        //reset the counter when it hits the number of stories per page
        if (counter >= divsByPage) {
            counter = 1;
            pageCounter++;
        }

        //turn all the stories off
        //stories[i].style.display = "none";

        //insert a new story under a page array
        pages.push({pageCounter:stories[i]});

        counter++;
    }

console.log(pages[1]);输出Object { pageCounter=[1]}

4

2 回答 2

0

如果要pageCounter用作变量而不是名称,则需要这样做:

var object = {};
object[pageCounter] = stories[i];
pages.push(object);
于 2012-07-05T12:54:43.637 回答
0

对象字段名称(即pageCounterin {pageCounter:stories[i]}is)始终是字符串(或类似字符串)。pageCounterin与你正在增加{pageCounter:stories[i]}无关。pageCounter

于 2012-07-05T12:55:04.383 回答