1

Is it possible to reuse a property during the declaration in JavaScript?

Example : phone_min: breakpoint.small_max + 1,

Code

var breakpoint = {
  small_max: 479,
  phone_min: breakpoint.small_max + 1,
};

I got error :

Uncaught TypeError: Cannot read property 'small_max' of undefined
madox2
  • 45,325
  • 15
  • 94
  • 95
Lofka
  • 169
  • 2
  • 9

2 Answers2

0

No, you cannot do that. In an object initializer, it's not possible to refer to the object that is "under construction".

Pointy
  • 389,373
  • 58
  • 564
  • 602
0

No it is not possible in JavaScript. You can save small_max in variable and then use it:

var small_max = 479;
var breakpoint = {
  small_max: small_max,
  phone_min: small_max + 1,
};
madox2
  • 45,325
  • 15
  • 94
  • 95