Giter Club home page Giter Club logo

Comments (6)

dop251 avatar dop251 commented on August 16, 2024

This is because currently for Go values that implement fmt.Stringer (and some others), toString and valueOf methods are added directly on the returned instance because otherwise it would use Object.prototype.toString which would return [object Object].

The easiest workaround is to delete the toString method from the instance before returning (by calling res.Delete("toString")). Not ideal, but I can't think of a better solution right now.

An alternative would be to create a special class GoObject which extends Object and use it as a prototype for wrapped values... Maybe not add it to the global object, similarly to the TypedArray constructor. It would give a benefit of being able to detect these values in JS code... I'll give it a thought.

from goja.

etiennemartin avatar etiennemartin commented on August 16, 2024

Hmm deleting would be a nice little work around for now. Unfortunately it doesn't seem to do anything. I've done the following:

res := r.ToValue(u).(*goja.Object)
err := res.Delete("toString")
if err != nil {
  panic("Failed to delete toString()")
}
res.SetPrototype(call.This.Prototype())

The code above returns a nil value to err yet if I call res.Get("toString") I still see the value set, and inspecting the value after the delete call shows that it's still set.

image

I'm not 100% what you mean about the GoObject but I'll take a look at TypedArray and see if I can see what you mean. Thanks for the quick reply!

We could also create a wrapper struct around the value we are using at the moment. It's a but more work but it would allow us to define our own implementation of fmt.Stringer for that wrapper. Not as clean, but it could also work.

from goja.

dop251 avatar dop251 commented on August 16, 2024

What you see is an internal field on a Go struct, it's not the same as the instance property of a javascript object. So, if you call new URL("...").toString() the prototype method should now be called. However, if you call ""+url, it won't be. I'll make some adjustments.

from goja.

etiennemartin avatar etiennemartin commented on August 16, 2024

That makes sense, for now I managed to get it working. I was using console.log(myVar); and it wasn't using toString but explicitly calling did do the right thing. This unblocks me for now at least! Thanks again for the feedback!

If you find a better solution, let me know.

from goja.

dop251 avatar dop251 commented on August 16, 2024

I've changed it so that the methods are no longer defined on the wrapped values, so no need to delete them anymore, it should just work.

from goja.

etiennemartin avatar etiennemartin commented on August 16, 2024

That's great! Thanks for the quick turn around!

from goja.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.