Replies: 2 comments
-
I think this should be exposed like something |
Beta Was this translation helpful? Give feedback.
0 replies
-
I think I got it to work (it was in the docs) package main
import (
"fmt"
"os"
"github.com/dop251/goja"
)
type document struct {
Item string
}
func (d *document) Check() {
fmt.Println(`~~> d.Item`, d.Item)
fmt.Printf("~~> d %p\n", d)
}
func documentC(call goja.ConstructorCall, rt *goja.Runtime) *goja.Object {
instance := &document{}
instanceValue := rt.ToValue(instance).(*goja.Object)
instanceValue.SetPrototype(call.This.Prototype())
return instanceValue
}
func main() {
runtime := goja.New()
runtime.Set("Document", documentC)
_, err := runtime.RunString(`
d1 = new Document();
d1.Item = "d1"
d1.Check()
d2 = new Document();
d2.Item = "d2"
d2.Check()
`)
if err != nil {
fmt.Println(err)
os.Exit(-1)
}
} I assume this is the most JavaScript-like way to initialise a struct. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
My goal is to expose (export) a structure from Go to JavaScript, so I can fill the fields and call methods on the structure. I can achieve this by creating a function (the
newdocfunc()
below). Is there another way to export the structure? The dumbnewdoc
below does not work because it creates just one object (of course).Beta Was this translation helpful? Give feedback.
All reactions