0

Hi I am getting an uncaught reference exception on my code.This is what I have:

 var config = {
    debug: true,
    data: {
        debug: false,
        logErrorsOnServer: true,
        defaultCulture: '',
        serviceUrl: ''
    },

    init: function(options) {
        if (!options) {
            return;
        }          
        if (options.hasOwnProperty('debug')) {
            data.debug = options.debug;
        }

    },
};

When I try to get the value of data.debug I get an uncaught reference error that says:

UncoughtReference Error: data is not defined

Whay can't I acces my data object?

5
  • 1
    Could be wrong here but try this.data.debug Commented Jun 19, 2013 at 10:59
  • What is going wrong. At a first glance everything okay in Chrome devtools Commented Jun 19, 2013 at 11:00
  • data.debug = options.debug throws UncoughtReference Error: data is not defined Commented Jun 19, 2013 at 11:00
  • I don't get it. You have config.debug, data.debug and options.hasOwnProperty('debug'). In my view, that's two to many. Commented Jun 19, 2013 at 11:03
  • Well, you have no variable data as far as I can see. If you want to to access the data property of the config object, you have to use config.data or this.data, assuming you call the function with config.init(). JavaScript doesn't have any hidden magic like Java where this. is implicit (and that's a good thing). It does exactly what you tell it to do (most of the time). Commented Jun 19, 2013 at 11:05

2 Answers 2

1

You need to say:

this.data.debug = options.debug;

...assuming that you are calling the init() function in a way that sets this to the (outer) object, e.g., with config.init().

Or you can say:

config.data.debug = options.debug;

The reason you got an error about data not being defined when you tried to use data.debug directly is that in fact data is not defined as a variable, it is a property of the object. Just because init() is a method on your object doesn't mean it automatically references other object properties.

Sign up to request clarification or add additional context in comments.

Comments

0

Well, the data variable is undefined. You probably want to use the object on your .data property of config (accessible via the this keyword):

…
    if (options.hasOwnProperty('debug')) {
        this.data.debug = options.debug;
    }
…

See also Javascript: Object Literal reference in own key's function instead of 'this' for different methods to access .data.

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.