jquery对象,如何将顶级值应用于子级?

时间:2012-04-09 14:59:14

标签: javascript

嗨我正在更新在object中创建的keyIndex数组,而我将数组添加到URL则不起作用:

我的代码:

var dataObject = {
    Indices: {
        subIndex: {
            keyIndex: [], //this is not updating in baseURL 'keyIndex'
            method: 'GetCCINationalIndicesData',
            baseURL: 'http://107.20.173.235/BlufinAPI/Service/ConsumerConfidenceIndex.svc/GetCCINationalIndicesData?InputJSON={"IndexID":"' + keyIndex + '","FromMonth":"10","FromYear":"2011","ToMonth":"3","ToYear":"2012"}'
        }
    },
    Geography: {
        0: '1',
        tiers: {
            method: 'GetCCITierIndicesData'
        },
        regions: {
            method: 'GetCCIRegionIndicesData'
        },
        city: {
            method: 'GetCCICityIndicesData'
        }
    },
    Demographics: {}
}

有什么不对吗?

1 个答案:

答案 0 :(得分:2)

这是因为keyIndex仅在创建baseURL字符串时被评估一次。

你可以改为baseURL一个函数......

baseURL:function() {
    return 'http://107.20.173.235/BlufinAPI/Service/ConsumerConfidenceIndex.svc/GetCCINationalIndicesData?InputJSON={"IndexID":"' + 
            this.keyIndex + 
            '","FromMonth":"10","FromYear":"2011","ToMonth":"3","ToYear":"2012"}';
}

然后把它称为函数......

dataObject.Indices.subIndex.baseURL();

虽然原始keyIndex首先不是对象属性的引用。

这与jQuery无关。