0

假设我有:

var test = {};
test.Data1 = {
    ...JSON objects here...
}; 
test.Data2 = {
    ...JSON objects here...
};
and so on... 

我通常通过以下方式访问这些 json 对象,然后进行一组调用:

this.scope.testData = test['Data1'];

但是,测试的数据可能越来越大,所以我只想将我想要的任何数据传递给函数并进行如下处理:

this.scope.setupData = function(data)
{
    var fData = test[data]; // is this line correct? 
    ...
    ...
    return fData;

};

但它不起作用。我得到:无法将未定义的属性“fData”设置为“[object Object]”......我是javaScript的新手,任何帮助将不胜感激。

4

1 回答 1

1

问题是里面的范围this.scope.setupData。要访问与您相关的变量,this.scope您需要this再次使用:

/**
 * At current scope, "this" refers to some object
 * Let's say the object is named "parent"
 *
 * "this" contains a property: "scope"
 */
this.scope.setupData = function(data)
{
    /**
     * At current scope, "this" refers to "parent.scope"
     *
     * "this" contains "setupData" and "testData"
     */
    var fData = this.testData[data]; // is this line correct? 
    ...
    ...
    return fData;
};
于 2013-07-01T04:45:53.380 回答