js

package
v0.0.0-...-cd3bf20 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 21, 2014 License: MIT, BSD-2-Clause Imports: 0 Imported by: 0

Documentation

Overview

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

Type conversions between Go types and JavaScript types are performed automatically according to the following table:

| Go types                       | Go interface type              | JavaScript type |
| ------------------------------ | ------------------------------ | --------------- |
| bool                           | bool                           | Boolean         |
| int?, uint?, float?            | float64                        | Number          |
| string                         | string                         | String          |
| [?]int8                        | []int8                         | Int8Array       |
| [?]int16                       | []int16                        | Int16Array      |
| [?]int32, [?]int               | []int                          | Int32Array      |
| [?]uint8                       | []uint8                        | Int8Array       |
| [?]uint16                      | []uint16                       | Int16Array      |
| [?]uint32, [?]uint, [?]uintptr | []uint                         | Int32Array      |
| [?]float32                     | []float32                      | Float32Array    |
| [?]float64                     | []float64                      | Float64Array    |
| all other slices and arrays    | []interface{}                  | Array           |
| maps, structs                  | map[string]interface{}         | Object          |
| functions                      | func(...interface{}) js.Object | Function        |
| time.Time                      | time.Time                      | Date            |

The second column denotes the types that are used when converting to `interface{}`. An exception are DOM elements, those are kept with the js.Object type. Additionally, a pointer to a named type is passed to JavaScript as an object which has wrappers for the type's exported methods. This can be used to provide getter and setter methods for Go fields to JavaScript.

Index

Constants

This section is empty.

Variables

View Source
var Arguments []Object

Arguments gives the value of JavaScript's "arguments" keyword. It can be used when passing Go functions to JavaScript as callbacks. Set this to a mock for testing with pure Go.

Functions

func Debugger

func Debugger()

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

func Keys

func Keys(o Object) []string

Keys returns the keys of the given JavaScript object.

Types

type Error

type Error struct {
	Object
}

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

func (*Error) Error

func (err *Error) Error() string

Error returns the message of the encapsulated JavaScript error object.

func (*Error) Stack

func (err *Error) Stack() string

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

type M

type M map[string]interface{}

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

type Object

type Object interface {

	// Get returns the object's property with the given key.
	Get(key string) Object

	// Set assigns the value to the object's property with the given key.
	Set(key string, value interface{})

	// Delete removes the object's property with the given key.
	Delete(key string)

	// Get returns the object's "length" property, converted to int.
	Length() int

	// Index returns the i'th element of an array.
	Index(i int) Object

	// SetIndex sets the i'th element of an array.
	SetIndex(i int, value interface{})

	// Call calls the object's method with the given name.
	Call(name string, args ...interface{}) Object

	// Invoke calls the object itself. This will fail if it is not a function.
	Invoke(args ...interface{}) Object

	// New creates a new instance of this type object. This will fail if it not a function (constructor).
	New(args ...interface{}) Object

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

	// Str returns the object converted to string according to JavaScript type conversions. Does not implement fmt.Stringer on purpose.
	Str() string

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

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

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

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

	// Interface returns the object converted to interface{}. See GopherJS' README for details.
	Interface() interface{}

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

	// IsUndefined returns true if the object is the JavaScript value "undefined".
	IsUndefined() bool

	// IsNull returns true if the object is the JavaScript value "null".
	IsNull() bool
}

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.

var Global Object

Global gives JavaScript's global object ("window" for browsers and "GLOBAL" for Node.js). Set this to a mock for testing with pure Go.

var Module 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", ...)'.

var This Object

This gives the value of JavaScript's "this" keyword. It can be used when passing Go functions to JavaScript as callbacks. Set this to a mock for testing with pure Go.

func InternalObject

func InternalObject(i interface{}) Object

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

type S

type S []interface{}

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

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL