0

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?

5
  • This shouldn't be a problem. You should show the code that isn't working. Commented May 2, 2019 at 15:34
  • JSON cannot contain functions. Commented May 2, 2019 at 15:34
  • 1
    @JonasWilms, it sounds like the OP wants the result of a function, like this.identifier: How can I assign the output of a function... Commented May 2, 2019 at 15:36
  • Could you please add the code that did not work (with the function) ? Commented May 2, 2019 at 15:39
  • Thank you for all the comments Jonas and Mark. Really do appreciate. The comment from @DanyalImran below solved my problem :-) Commented May 2, 2019 at 18:28

2 Answers 2

2

You don't need a function, an expression is just fine to solve your problem

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'][Math.round(Math.random())];
    }
  }

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)

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you Danyal. Your solution worked. I'll try it out with this.motabolism and the rest. Really do appreciate :-)
No problem, glad I could help. It this solution worked, mark it as verified so someone in future who might get stuck on this problem will know how resolve it :) @SnoobySchool
1

You can just assign a function as any other value

  this.test = function() { };

you will then be able to call it as:

  new Bluehuman().test();

and if you log it to the console directly, you'll also see it:

  console.log(new Bluehuman());

If you however call JSON.stringify on it, it will be turned into a string containing only data, functions (and a lot of other things) get removed.

1 Comment

Thank you Jonas. I'll be using your suggestion for the update portion.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.