Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reference a JavaScript object key within the object? [duplicate]

Tags:

javascript

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/',
}
}
like image 710
Matt Avatar asked Dec 11 '22 17:12

Matt


1 Answers

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.

like image 97
Explosion Pills Avatar answered May 20 '23 23:05

Explosion Pills