#Class Variables

17 messages · Page 1 of 1 (latest)

flint hollow
#
let message;
class Test {
  //message = "Hi";
  constructor() {
    someFunction.load(model, async function() {
      message = "Hello world 2"
    })
    this.msg = message
  }
}
const random = new Test();
console.log(random.msg) //undefined

The variable's value is obtained from inside the someFunction, and placing this.msg = message statement outside the constructor does not work either. I need to access the message property after its value is declared inside the someFunction and I dont seem to be able to find any resources.
Kindly help.

proper meadowBOT
#
  • Consider reading #how-to-get-help to improve your question!
  • Explain what exactly your issue is.
  • Post the full error stack trace, not just the top part!
  • Show your code!
  • Issue solved? Press the button!
  • Marked as resolved by OP
cedar drift
flint hollow
#

The thing is that the Test class has 4 parametres which the code inside the async function needs as well

#

Ill give it a try

flint hollow
exotic yacht
flint hollow
cedar drift
cedar drift
#

this.msg = message; this doesnt create a reference to the message variable, it just assigns its value to this.msg

flint hollow
#

Ohhh

cedar drift
#

edit this.msg or log message

flint hollow
#

Alright

#

I shifted the code back to my original file, actually
Ill definitely try it, though
Thanks for the help!

exotic yacht
#

also consider that constructors aren't supossed to have side effects, you shouldn't run actual logic other than the one related to property initialization

#

it's better for you to do new Test().load() instead of calling load on the constructor