0

In a Gulp.js file (but it can be any Javascript file) I set the paths in a Javascript object:

var paths = {
    devDir : 'builds/development/',
    devDirGlobs : this.devDir+'*.html'              
}

Now, as you can see, I'm trying to recall the property "devDir" in the value of the property "devDirGlobs", as "this.devDir".

It doesn't work, but also it doesn't give to me any error?

Any hint?

Thanks in advance for your help!

Andy
  • 61,948
  • 13
  • 68
  • 95
AmintaCode
  • 364
  • 5
  • 15
  • You can’t do that. Sorry. You’ll have to assign to the other property separately, or put the value of `devDir` in a variable beforehand. – Ry- Nov 20 '15 at 11:24
  • 3
    It won't work but this may point you in the right direction - http://stackoverflow.com/questions/4616202/self-references-in-object-literal-declarations – Tim Sheehan Nov 20 '15 at 11:25
  • thanks minitech I've used your solution, equivalent to dan-prince solution! And thanks to dontfeedthecode for pointing me out the full explanation, as Yoshi right mark of duplicate! – AmintaCode Nov 20 '15 at 12:29

1 Answers1

1

It's not possible to access the properties of the object before the object has been declared.

However, you could construct the object with dot syntax rather than as a literal.

var paths = {}
paths.devDir = 'builds/development/';
paths.devDirGlobs = paths.devDir + '*.html';              

Or move common values into a shared config object.

var config = {
  dir: 'builds/development'
};

var paths = {    
  devDir : config.dir,
  devDirGlobs : config.dir + '*.html'              
};
Dan Prince
  • 29,491
  • 13
  • 89
  • 120
  • Thanks! This is very useful if I need to make more than one property depends on another property and avoids the natural "function solution"! – AmintaCode Nov 20 '15 at 12:24