js
3 分钟阅读
js
https://pkg.go.dev/syscall/js@go1.20.1
Package js gives access to the WebAssembly host environment when using the js/wasm architecture. Its API is based on JavaScript semantics.
This package is EXPERIMENTAL. Its current scope is only to allow tests to run, but not yet to provide a comprehensive API for users. It is exempt from the Go compatibility promise.
常量
This section is empty.
变量
This section is empty.
函数
func CopyBytesToGo <- go1.13
|
|
CopyBytesToGo copies bytes from src to dst. It panics if src is not an Uint8Array or Uint8ClampedArray. It returns the number of bytes copied, which will be the minimum of the lengths of src and dst.
func CopyBytesToJS <- go1.13
|
|
CopyBytesToJS copies bytes from src to dst. It panics if dst is not an Uint8Array or Uint8ClampedArray. It returns the number of bytes copied, which will be the minimum of the lengths of src and dst.
类型
type Error
|
|
Error wraps a JavaScript error.
(Error) Error
|
|
Error implements the error interface.
type Func <- go1.12
|
|
Func is a wrapped Go function to be called by JavaScript.
func FuncOf <- go1.12
|
|
FuncOf returns a function to be used by JavaScript.
The Go function fn is called with the value of JavaScript’s “this” keyword and the arguments of the invocation. The return value of the invocation is the result of the Go function mapped back to JavaScript according to ValueOf.
Invoking the wrapped Go function from JavaScript will pause the event loop and spawn a new goroutine. Other wrapped functions which are triggered during a call from Go to JavaScript get executed on the same goroutine.
As a consequence, if one wrapped function blocks, JavaScript’s event loop is blocked until that function returns. Hence, calling any async JavaScript API, which requires the event loop, like fetch (http.Client), will cause an immediate deadlock. Therefore a blocking function should explicitly start a new goroutine.
Func.Release must be called to free up resources when the function will not be invoked any more.
Example
|
|
(Func) Release <- go1.12
|
|
Release frees up resources allocated for the function. The function must not be invoked after calling Release. It is allowed to call Release while the function is still running.
type Type
|
|
Type represents the JavaScript type of a Value.
|
|
(Type) String
|
|
type Value
|
|
Value represents a JavaScript value. The zero value is the JavaScript value “undefined”. Values can be checked for equality with the Equal method.
func Global
|
|
Global returns the JavaScript global object, usually “window” or “global”.
func Null
|
|
Null returns the JavaScript value “null”.
func Undefined
|
|
Undefined returns the JavaScript value “undefined”.
func ValueOf
|
|
ValueOf returns x as a JavaScript value:
| Go | JavaScript |
| ---------------------- | ---------------------- |
| js.Value | [its value] |
| js.Func | function |
| nil | null |
| bool | boolean |
| integers and floats | number |
| string | string |
| []interface{} | new array |
| map[string]interface{} | new object |
Panics if x is not one of the expected types.
(Value) Bool
|
|
Bool returns the value v as a bool. It panics if v is not a JavaScript boolean.
(Value) Call
|
|
Call does a JavaScript call to the method m of value v with the given arguments. It panics if v has no method m. The arguments get mapped to JavaScript values according to the ValueOf function.
(Value) Delete <- go1.14
|
|
Delete deletes the JavaScript property p of value v. It panics if v is not a JavaScript object.
(Value) Equal <- go1.14
|
|
Equal reports whether v and w are equal according to JavaScript’s === operator.
(Value) Float
|
|
Float returns the value v as a float64. It panics if v is not a JavaScript number.
(Value) Get
|
|
Get returns the JavaScript property p of value v. It panics if v is not a JavaScript object.
(Value) Index
|
|
Index returns JavaScript index i of value v. It panics if v is not a JavaScript object.
(Value) InstanceOf
|
|
InstanceOf reports whether v is an instance of type t according to JavaScript’s instanceof operator.
(Value) Int
|
|
Int returns the value v truncated to an int. It panics if v is not a JavaScript number.
(Value) Invoke
|
|
Invoke does a JavaScript call of the value v with the given arguments. It panics if v is not a JavaScript function. The arguments get mapped to JavaScript values according to the ValueOf function.
(Value) IsNaN <- go1.14
|
|
IsNaN reports whether v is the JavaScript value “NaN”.
(Value) IsNull <- go1.14
|
|
IsNull reports whether v is the JavaScript value “null”.
(Value) IsUndefined <- go1.14
|
|
IsUndefined reports whether v is the JavaScript value “undefined”.
(Value) Length
|
|
Length returns the JavaScript property “length” of v. It panics if v is not a JavaScript object.
(Value) New
|
|
New uses JavaScript’s “new” operator with value v as constructor and the given arguments. It panics if v is not a JavaScript function. The arguments get mapped to JavaScript values according to the ValueOf function.
(Value) Set
|
|
Set sets the JavaScript property p of value v to ValueOf(x). It panics if v is not a JavaScript object.
(Value) SetIndex
|
|
SetIndex sets the JavaScript index i of value v to ValueOf(x). It panics if v is not a JavaScript object.
(Value) String
|
|
String returns the value v as a string. String is a special case because of Go’s String method convention. Unlike the other getters, it does not panic if v’s Type is not TypeString. Instead, it returns a string of the form “
(Value) Truthy <- go1.12
|
|
Truthy returns the JavaScript “truthiness” of the value v. In JavaScript, false, 0, “”, null, undefined, and NaN are “falsy”, and everything else is “truthy”. See https://developer.mozilla.org/en-US/docs/Glossary/Truthy.
(Value) Type
|
|
Type returns the JavaScript type of the value v. It is similar to JavaScript’s typeof operator, except that it returns TypeNull instead of TypeObject for null.
type ValueError
|
|
A ValueError occurs when a Value method is invoked on a Value that does not support it. Such cases are documented in the description of each method.
(*ValueError) Error
|
|