diff --git a/js/js_notwasm.go b/js/js_notwasm.go index fe57106..d377e51 100644 --- a/js/js_notwasm.go +++ b/js/js_notwasm.go @@ -63,12 +63,14 @@ type Value struct { } var ( - id *js.Object + id *js.Object + instanceOf *js.Object ) func init() { if js.Global != nil { id = js.Global.Call("eval", "(function(x) { return x; })") + instanceOf = js.Global.Call("eval", "(function(x, y) { return x instanceof y; })") } } @@ -171,6 +173,10 @@ func (v Value) String() string { return v.v.String() } +func (v Value) InstanceOf(t Value) bool { + return instanceOf.Invoke(v, t).Bool() +} + func GetInternalObject(v Value) interface{} { return v.v } diff --git a/js/js_test.go b/js/js_test.go index d446894..60e5274 100644 --- a/js/js_test.go +++ b/js/js_test.go @@ -64,3 +64,43 @@ func TestInt64(t *testing.T) { t.Errorf("got %#v, want %#v", got, want) } } + +func TestInstanceOf(t *testing.T) { + arr := js.Global.Call("eval", "[]") + got := arr.InstanceOf(js.Global.Call("eval", "Array")) + want := true + if got != want { + t.Errorf("got %#v, want %#v", got, want) + } + + got = arr.InstanceOf(js.Global.Call("eval", "Object")) + want = true + if got != want { + t.Errorf("got %#v, want %#v", got, want) + } + + got = arr.InstanceOf(js.Global.Call("eval", "String")) + want = false + if got != want { + t.Errorf("got %#v, want %#v", got, want) + } + + str := js.Global.Call("eval", "String").New() + got = str.InstanceOf(js.Global.Call("eval", "Array")) + want = false + if got != want { + t.Errorf("got %#v, want %#v", got, want) + } + + got = str.InstanceOf(js.Global.Call("eval", "Object")) + want = true + if got != want { + t.Errorf("got %#v, want %#v", got, want) + } + + got = str.InstanceOf(js.Global.Call("eval", "String")) + want = true + if got != want { + t.Errorf("got %#v, want %#v", got, want) + } +}