I am very new to JS.
I am trying to define properties of a variable, but the trick is that I want JS to define a new variable while defining another.
This does not work:
var robber = {
health: 10,
halfHealth: this.health/2,
};
I expect robber.halfHealth
to be 5, but answer is NaN
. I guess it does it because var robber
is not really defined by the time attempt to calculate halfHealth
is done?
If I put it an another way it works:
var robber = {
health: 10,
// halfHealth: this.health/2,
};
var robberHalfHealth = robber.health/2;
I do not want to have hundreds of variables, but want all variables related to "robber" to live {in one house}, so to say.
P.S. One of ways might be to add function which would define halfHealth
and do robber.init()
, but is there a more straightforward solution?