0

Let's say I have:

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

I usually access these json objects followed by a set of calls by:

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

However, may data for test is getting larger so i just wanted to pass whatever data i want to a function and do the processing like:

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

};

but it's not working. I'm getting :Cannot set property "fData" of undefined to "[object Object]" ... I'm new to javaScript, any help would be appreciated.

3
  • 1
    That's the exact wording of the error? Sure seems like you're leaving out a bunch of important information. Commented Jul 1, 2013 at 4:41
  • Looks like you're trying to use data as a property name... but data is an Object. Commented Jul 1, 2013 at 4:44
  • 2
    How do you call setupData ? Commented Jul 1, 2013 at 4:45

1 Answer 1

1

The problem is the scope inside this.scope.setupData. To access the variables relate to this.scope you need to use this again:

/**
 * 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;
};
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.