Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[FEATURE REQUEST] Assignments in struct literal #236

Open
@Archs

Description

@Archs

I know now the js tag works fine, but the struct field has to be accessed directly to be effective, and thus the struct literal #114 (comment) would not work declaratively, for example:

a := &A{
        Object: js.Global.Get("Object").New(),
        Method: func() {
            println("hello")
        },
    }
a.Method()  // This gives: Uncaught TypeError: a.Object.Method is not a function.

but the following code is ok:

a := &A{
        Object: js.Global.Get("Object").New(),
    }
a.Method = func() {
    println("hello")
}
a.Method() // this is ok

This troubles me for a quite long time, could assignments in struct literal works directly?

I think this feature is useful and would be friendly to the beginners of gopherjs, for:

  1. it's more compatible with Golang itself
  2. when using the native js libraries, it's easier to supply constructor options as a struct literal
    than to declare a struct variable first and then to set each field separately
  3. you can be more declarative 😄

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions