I'm building a small world builder in JavaScript (part of a larger simulation).
I'm trying to define an object's property in the constructor function by assigning a functions output to it.
In the code below, 'this.identifier' executes like a charm, but I want to assign more complex functions to for instance 'this.gender'.
In 'this.gender' I want to use math.random.math.floor to cycle through an array (that has two values, male and female).
When I write the actual function, 'this.gender' is dropped from the new Human object.
{
"identifier":42,
"lifestate":"Alive",
"health":"100",
"age":"10",
"metabolism":"12",
"econsumption":"11111",
"parent":"yes"
}
- gender is dropped the moment I change it to a function.
I've tried using a return statement, but it makes no difference.
class Bluehuman {
constructor() {
this.identifier = Math.floor((Math.random() * 100));
this.lifestate = 'Alive';
this.health = '100';
this.age = '10';
this.metabolism = ['Low','Medium','High'];
this.econsumption = '11111';
this.parent = ['Yes','No'];
this.gender = ['Male','Female']; // Want to change this to a function without dropping from the new Bleuhuman object
}
}
var bluehuman = {};
var bluehumans = [];
for (var i = 0; i < 10; i++) {
bluehuman[i] = new Bluehuman();
bluehumans.push(bluehuman[i]);
}
var arrayPrint = JSON.stringify(bluehumans);
console.log(arrayPrint)
How can I assign the output of a function to 'this.gender' without having it dropped from the new bluehuman object?
this.identifier:How can I assign the output of a function...