Embedding spread operator during es7 declaration

I want to do something like this:

const translations = {
    en: {
        statistics_title: 'Statistics'
    },
    ru: {
        statistics_title: ''
    },
    uk: {
        ...ru
    }
}

obviously this won't work, of course, I can set another const and then use it, but I'm lazy), so I wonder if javascript allows you to do this?

+4
source share
2 answers

You cannot refer to objects yourself if this is not a method.

You can do:

const translations = {
    en: {
        statistics_title: 'Statistics'
    },
    ru: {
        statistics_title: ''
    },
    get uk () {
        return this.ru
    }
}

Edit

If you want to translations.uknot copy ruor store your own data, this method will not work. It just sets translations.ukas a link totranslations.ru

+1
source

You must declare this value rubefore distribution, i.e.:

const ru = {
    statistics_title: ''
};

const translations = {
    en: {
        statistics_title: 'Statistics'
    },
    ru,
    uk: {
        ...ru
    }
};
+1
source

Source: https://habr.com/ru/post/1689303/


All Articles