Js

js

Package js provides functions for interacting with native JavaScript APIs.

import "github.com/gopherjs/gopherjs/js"

Package js provides functions for interacting with native JavaScript APIs. Calls to these functions are treated specially by GopherJS and translated directly to their corresponding JavaScript syntax.

Use MakeWrapper to expose methods to JavaScript. Use MakeFullWrapper to expose methods AND fields to JavaScript. When passing values directly, the following type conversions are performed:

| Go type               | JavaScript type       | Conversions back to any |
| --------------------- | --------------------- | ----------------------- |
| bool                  | Boolean               | bool                    |
| integers and floats   | Number                | float64                 |
| string                | String                | string                  |
| []int8                | Int8Array             | []int8                  |
| []int16               | Int16Array            | []int16                 |
| []int32, []int        | Int32Array            | []int                   |
| []uint8               | Uint8Array            | []uint8                 |
| []uint16              | Uint16Array           | []uint16                |
| []uint32, []uint      | Uint32Array           | []uint                  |
| []float32             | Float32Array          | []float32               |
| []float64             | Float64Array          | []float64               |
| all other slices      | Array                 | []any                   |
| arrays                | see slice type        | see slice type          |
| functions             | Function              | func(...any) *js.Object |
| time.Time             | Date                  | time.Time               |
| -                     | instanceof Node       | *js.Object              |
| maps, structs         | instanceof Object     | map[string]any          |

Additionally, for a struct containing a *js.Object field, only the content of the field will be passed to JavaScript and vice versa.

Variables

Global gives JavaScript's global object ("window" for browsers and "GLOBAL" for Node.js).

Source: js/js.go:98

var Global *Object

Module gives the value of the "module" variable set by Node.js. Hint: Set a module export with 'js.Module.Get("exports").Set("exportName", ...)'.

Note that js.Module is only defined in runtimes which support CommonJS modules (https://nodejs.org/api/modules.html). NodeJS supports it natively, but in browsers it can only be used if GopherJS output is passed through a bundler which implements CommonJS (for example, webpack or esbuild).

Source: js/js.go:107

var Module *Object

Undefined gives the JavaScript value "undefined".

Source: js/js.go:110

var Undefined *Object

Functions

func Debugger()

Source: js/js.go:113

Debugger gets compiled to JavaScript's "debugger;" statement.

func InternalObject(i any) *Object

Source: js/js.go:116

InternalObject returns the internal JavaScript object that represents i. Not intended for public use.

func Keys(o *Object) []string

Source: js/js.go:126

Keys returns the keys of the given JavaScript object.

func MakeFullWrapper(i any) *Object

Source: js/js.go:163

MakeFullWrapper creates a JavaScript object which has wrappers for the exported methods of i, and, where i is a (pointer to a) struct value, wrapped getters and setters (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty) for the non-embedded exported fields of i. Values accessed via these methods and getters are themselves wrapped when accessed, but an important point to note is that a new wrapped value is created on each access.

func MakeFunc(fn func(this *Object, arguments []*Object) any) *Object

Source: js/js.go:121

MakeFunc wraps a function and gives access to the values of JavaScript's "this" and "arguments" keywords.

func MakeUint64(hi, lo float64) uint64

Source: js/js.go:265

MakeUint64 will take the high and low parts to construct a uint64 number. Since JS doesn't have a 64-bit integer we store it as a high and low 32-bit integers.

func MakeWrapper(i any) *Object

Source: js/js.go:139

MakeWrapper creates a JavaScript object which has wrappers for the exported methods of i. Use explicit getter and setter methods to expose struct fields to JavaScript.

func NewArrayBuffer(b []byte) *Object

Source: js/js.go:250

NewArrayBuffer creates a JavaScript ArrayBuffer from a byte slice.

func Uint64High(x uint64) uint32

Source: js/js.go:268

Uint64High will get the high 32-bit integer used when storing uint64.

func Uint64Low(x uint64) uint32

Source: js/js.go:271

Uint64Low will get the low 32-bit integer used when storing uint64.

Types

type Error

Source: js/js.go:83

Error encapsulates JavaScript errors. Those are turned into a Go panic and may be recovered, giving an *Error that holds the JavaScript error object.

type Error struct {
	*Object
}
Fields
  • *Object

func Error) Error() string

Source: js/js.go:88

Error returns the message of the encapsulated JavaScript error object.

func Stack() string

Source: js/js.go:93

Stack returns the stack property of the encapsulated JavaScript error object.

type M

Source: js/js.go:258

M is a simple map type. It is intended as a shorthand for JavaScript objects (before conversion).

type M map[string]any

type Object

Source: js/js.go:29

Object is a container for a native JavaScript object. Calls to its methods are treated specially by GopherJS and translated directly to their JavaScript syntax. A nil pointer to Object is equal to JavaScript's "null". Object can not be used as a map key.

type Object struct {
	// contains filtered or unexported fields
}

func Bool() bool

Source: js/js.go:59

Bool returns the object converted to bool according to JavaScript type conversions.

func Call(name string, args ...any) *Object

Source: js/js.go:50

Call calls the object's method with the given name.

func Delete(key string)

Source: js/js.go:38

Delete removes the object's property with the given key.

func Float() float64

Source: js/js.go:74

Float returns the object converted to float64 according to JavaScript type conversions (parseFloat).

func Get(key string) *Object

Source: js/js.go:32

Get returns the object's property with the given key.

func Index(i int) *Object

Source: js/js.go:44

Index returns the i'th element of an array.

func Int() int

Source: js/js.go:65

Int returns the object converted to int according to JavaScript type conversions (parseInt).

func Int64() int64

Source: js/js.go:68

Int64 returns the object converted to int64 according to JavaScript type conversions (parseInt).

func Interface() any

Source: js/js.go:77

Interface returns the object converted to any. See table in package comment for details.

func Invoke(args ...any) *Object

Source: js/js.go:53

Invoke calls the object itself. This will fail if it is not a function.

func Length() int

Source: js/js.go:41

Length returns the object's "length" property, converted to int.

func New(args ...any) *Object

Source: js/js.go:56

New creates a new instance of this type object. This will fail if it not a function (constructor).

func Set(key string, value any)

Source: js/js.go:35

Set assigns the value to the object's property with the given key.

func SetIndex(i int, value any)

Source: js/js.go:47

SetIndex sets the i'th element of an array.

func String() string

Source: js/js.go:62

String returns the object converted to string according to JavaScript type conversions.

func Uint64() uint64

Source: js/js.go:71

Uint64 returns the object converted to uint64 according to JavaScript type conversions (parseInt).

func Unsafe() uintptr

Source: js/js.go:80

Unsafe returns the object as an uintptr, which can be converted via unsafe.Pointer. Not intended for public use.

type S

Source: js/js.go:261

S is a simple slice type. It is intended as a shorthand for JavaScript arrays (before conversion).

type S []any