go: syscall/js: comparing two number objects with `==` fails
(This issue is copied from https://github.com/neelance/go/issues/27)
I’m using 8d6062b08b6d737f773d697e5dc52d98735b1194 on neelance’s wasm-wip branch
package main
import (
"syscall/js"
)
func main() {
n := js.Global.Get("Number")
println(n.New(1) == n.New(1))
}
Shows false
, I expected true
though. I suggest ==
return true
since it’d be more intuitive.
The current result might be intended, but was really confusing to me. For example, getting WebGL’s constant like FRAMEBUFFER_COMPLETE
via Get creates a number object if I understand correctly, and comparing this constant value and another value always fails.
// gl is a WebGL context of a canvas.
if s := gl.Call("checkFramebufferStatus", gl.Get("FRAMEBUFFER")); s != gl.Get("FRAMEBUFFER_COMPLETE") {
// This always comes here, whatever s is!
return js.Null, errors.New(fmt.Sprintf("opengl: creating framebuffer failed: %d", s.Int()))
}
String object might be in the same situation .
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Comments: 17 (16 by maintainers)
We need to add
func Equal(a, b js.Value) bool
to thesyscall/js
package. It is not possible to compare twojs.Value
with the==
operator.I just got an initial idea on how to make Go’s
==
work forjs.Value
. This solution might also give performance benefits in other situations.Afaik there are no JS function equivalents of
==
and===
. Also, checking two values for equality is a common case, so it should be part of the core API.Yes,
Equal
would be JS’===
, since this is what fits Go’s semantics of equality the best. My preference would be to not offer==
, because it is considered bad practice by many.