让我说我有:
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的新手,任何帮助都将不胜感激。
答案 0 :(得分: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;
};