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
  • 49,493
  • 17
  • 99
  • 99
Lofka
  • 179
  • 1
  • 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
  • 405,095
  • 59
  • 585
  • 614
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
  • 49,493
  • 17
  • 99
  • 99