Skip to content
Advertisement

How to set Object Value equals Object value inside variable

I have a variable set to an object:

var myConfig = {
  elementID: "Test",
  cookieName: "myTest",
  cookieExpiresAfterDays: 1,
  cookieDomain: ".localhost:3000/",
  privacyPolicy: "",

  services: [
    {
      name: "GoogleAnalytics",
      default: !0,
      required: !0,
      onlyOnce: !0,
    },
  ],

  translations: {
    GoogleAnalytics: {
      description: `<p>CookieDomain: ${cookieDomain}</p>`,
      title: "Google Analytics",
    },
  }
};

In this block:

 translations: {
    GoogleAnalytics: {
      description: `<p>CookieDomain: ${cookieDomain}</p>`,
      title: "Google Analytics",
    },
  }

I need to set cookieDomain: cookieDomain but it returns me an error

Uncaught ReferenceError: cookieDomain is not defined

and when I try cookieDomain: this.cookieDomain it prints undefined.

How can I access the correct value?

Advertisement

Answer

you have two solutions here either

  1. define the cookieDomain variable in the same file before the initializing myConfig object

  2. pass the cookieDomain value as a parameter so the myConfig object would be

var myConfig = {
  elementID: "Test",
  cookieName: "myTest",
  cookieExpiresAfterDays: 1,
  cookieDomain: ".localhost:3000/",
  privacyPolicy: "",

  services: [
    {
      name: "GoogleAnalytics",
      default: !0,
      required: !0,
      onlyOnce: !0,
    },
  ],

  translations: ({cookieDomain}) => {
    GoogleAnalytics: {
      description: `<p>CookieDomain: ${cookieDomain}</p>`,
      title: "Google Analytics",
    },
  }
};

and then you need to call the translations block by

myConfig.translations({cookieDomain: 'some value here'});
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement