How come `state` in this code would show as `state...
# javascript
j
How come
state
in this code would show as
state_v23xwe$_0
in javascript?
Copy code
@Serializable
data class StateAware(@JsName("id") val id: String = hashCode().toString(), @JsName("test")val test: String = "Tested") {

    @JsName("state")
    var state: Int = Initilized
        set(value) {
            println("Item with id:$id -> Old State: '${this.state}' | New State: '$value' has been set")
            field = value
            stateAwareContainer.sync(this)
        }
}
r
Out of curiosity: did you try
Copy code
@get:JsName("getState")
@set:JsName("state")
?
s
state_v23xwe$_0
is likely a backing
field
. You will also have
state
JS property on the object prototype.
j
@r4zzz4k Seems like nothing happens
@Svyatoslav Kuzmich [JB] Not sure what that translates to on the code? I mean the properties in the data class constructor works. But if they are defined inside the data class it does not?
Im guessing the setter does screw things up for me
s
Properties should work.
val state
will be translated to something like:
Copy code
Object.defineProperty(StateAware.prototype, 'state', {
    get: function () {
      return this.state_v23xwe$_0;
    },
    set: function (value) {
      // ...
      this.state_v23xwe$_0 = value;
      // ...
    }
  })
And on the use-site you can do:
Copy code
var sa = new StateAware();
sa.state = 10;
What’s not working?
j
Ah now I see, thanks for clearing that up for me 🙂