0

I am declaring:

var ENV       = {
    VERSION: 1,
    SERVER: 'midomain.com',
    SERVER_DEV: 'testdomain.dev',
    API_ROUTE: 'socket/',
    API: (false) ? this.SERVER + '/' + this.API_ROUTE : this.SERVER_DEV + '/' + this.API_ROUTE };

should get:

{ VERSION: 1,SERVER: 'midomain.com',SERVER_DEV: 'testdomain.dev',API_ROUTE: 'socket/', API: 'testdomain.dev/socket/' }

but I get

{ VERSION: 1,SERVER: 'midomain.com',SERVER_DEV: 'testdomain.dev',API_ROUTE: 'socket/',  API: 'undefined/undefined' }
WITO
  • 445
  • 6
  • 13
  • use an object and a constructor, see https://stackoverflow.com/questions/5030739/javascript-how-to-define-a-constructor. You pass the version, the server, the API route and you build the API from it. Note that with that you can extract the server_dev outside of it. – Walfrat Oct 03 '17 at 08:12

1 Answers1

0

Because in that case context is global, not an your object. You can check it with an example:

window.b = 666;

let a = {
 prop: this.b
}

console.log(a); // display: {prop: 666}

You can fill your object properties one-by-one

let ENV       = {};
ENV.VERSION = 1;
ENV.SERVER = 'midomain.com';
ENV.API = ENV.SERVER;

or create function with 'new' syntax

var ENV = new function() {
   this.VERSION = 1;
   this.SERVER = 'midomain.com';
   this.API = this.SERVER;
};
Artem Kolodko
  • 410
  • 2
  • 10
  • 1
    Thanks, I used const ENV = new function () { this.VERSION = 1; this.SERVER = 'domain.com'; this.SERVER_DEV = 'testdomain.dev'; this.API_ROUTE = 'socket/'; this.API = (process.env.PROD) ? this.SERVER + '/' + this.API_ROUTE : this.SERVER_DEV + '/' + this.API_ROUTE; return this; }(); – WITO Oct 03 '17 at 08:47