How to reference a JavaScript object key within the object?

javascript

Solution

You could use a method:

window.EXAMPLE = {
    config : {
        theme: 'example',
        image_path: function () {
            return '/wp-content/themes/' + this.theme + '/img/';
        },
    }
}

Of course, then you have to access it via `EXAMPLE.config.image_path()`

You should probably not be defining things on `window` either, and just use whatever the current scope is.

Problem

How would I go about referencing a JavaScript hash key within the object itself? Fore example, I want to be able to actually make use of the "theme" key. Would I use "this" to reference "theme"? ``` window.EXAMPLE = { config : { theme: 'example', image_path: '/wp-content/themes/' + this.theme + '/img/', } } ```

Original source

Related problems