MCPcopy Create free account
hub / github.com/dop251/goja / ExampleRuntime_ExportTo_func

Function ExampleRuntime_ExportTo_func

runtime_test.go:896–917  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

894}
895
896func ExampleRuntime_ExportTo_func() {
897 const SCRIPT = `
898 function f(param) {
899 return +param + 2;
900 }
901 `
902
903 vm := New()
904 _, err := vm.RunString(SCRIPT)
905 if err != nil {
906 panic(err)
907 }
908
909 var fn func(string) string
910 err = vm.ExportTo(vm.Get("f"), &fn)
911 if err != nil {
912 panic(err)
913 }
914
915 fmt.Println(fn("40")) // note, _this_ value in the function will be undefined.
916 // Output: 42
917}
918
919func ExampleRuntime_ExportTo_funcThrow() {
920 const SCRIPT = `

Callers

nothing calls this directly

Calls 4

NewFunction · 0.85
RunStringMethod · 0.80
ExportToMethod · 0.80
GetMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…