Skip to content

Arrays in JS objects don't seem to work as I'd expect #460

Closed
@theclapp

Description

@theclapp
o := struct {
    *js.Object
    a []int `js:"a"`
    b js.M  `js:"b"`
}{Object: js.Global.Get("Object").New()}
o.a = []int{1}
o.b = js.M{"x": "x"}
fmt.Printf("%v ", o.a[0])
o.a[0] = 2 // <== this doesn't work
fmt.Printf("%v ", o.a[0])

o.Get("a").SetIndex(0, 3) // <== this works but is awkward
fmt.Printf("%v\n", o.a[0])

fmt.Printf("%v ", o.b["x"])
o.b["x"] = "y" // <== this doesn't work either
fmt.Printf("%v ", o.b["x"])

o.Get("b").Set("x", "z") // <== works but awkward
fmt.Printf("%v ", o.b["x"])

result:
1 1 3
x x z

Expected:
1 2 3
x y z

Playground

As near as I can tell, the array "a" gets $internalize()'ed into a whole new array, which is assigned to and then thrown away. Similarly, "b" gets $internalized()'ed into a whole new object, which is assigned to and then thrown away.

Should one just not put slices or maps in JS objects?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions