Is it possible in JS to set a reference to update text?

I have a score system that I would like to ‘Pass a reference to’ to automatically update a text on change.
Something like this, myTextBox.text = &score;

And when score changes so does the text field, can this be done with either passing a reference to the property or by an observer?

I don’t think this is possible in js… just add myTextBox.text = score; after score changes.

If the score system is contained in a object, you could quite easily use getter/setter methods.
I’m on my phone, but google Object.defineProperty

Hey Weird, I’m curious what benefit this would have over myTextBox.text = score; I’m looking basically for a event or a listener to automatically set the value once changed through code.

defineProperty will define a new property to an object, I’m just looking to update an existing objects property.

Here’s a small sample using the global “window” object as “parent”/container.
The comments should explain, but if any doubt, just ask.
https://playground.babylonjs.com/#P1S5JY

1 Like

Now I understand what you’re suggesting, it’s just a property setter, I’ll end up using that.

I thought defineProperty applied a property to the object once called.

Thanks so much.

1 Like