-1

I have a object where I would like to use value of one key in the value of another key

var paths = {
  src = '/home'
  styles: src + '/styles',
  scripts: src + '/scripts'
}

This does not work.

var paths = {
  src = '/home'
  styles: this.src + '/styles',
  scripts: this.src + '/scripts'
  ...
}

Does not work either as this relates to global scope not paths scope.

this works but would rather not have multiple objects

var app  = {
  src: '/home'
}

var paths = {
  styles: app.src + '/styles',
  scripts: app.src + '/scripts'
  ...
}
otissv
  • 815
  • 1
  • 10
  • 17

2 Answers2

1

Construct a simple function, with src as a parameter and return the object, like this

function createPaths(source) {
    return {
        src: source,
        styles: source + '/styles',
        scripts: source + '/scripts'
    };
}

And you can get the object by calling the function, like this

var app = createPaths('/home');
thefourtheye
  • 233,700
  • 52
  • 457
  • 497
1

var paths = {
  src: '/home'
};
paths.styles = paths.src + '/styles';
paths.scripts = paths.src + '/scripts';
console.log(paths);