siongui/userpages

Extend the Synonyms post to include javascript "new"

pbrown12303 opened this issue · 2 comments

In using existing javascript one of the common activities is to instantiate (from go) a class defined in the existing javascript. I struggled with this until I came across this post that explains exactly what the javascript "new" does. I have been able to implement each of the steps described in the post with gopherjs, thereby replicating "new" in go, but it took me a long time to figure it all out.

See my commit. If this is not what you need, re-open the issue and give me more details.

Yes, this is excellent, but there was another aspect that was confusing me that could benefit from your posts (I am new to both gopherjs and javascript, so I'm climbing the learning curve). The original piece of javascript I was trying to re-create in go was

var x = new joint.dia.Graph

I had tried (unsuccessfully) to do:

x := js.Global.Get("joint.dia.Graph").New()

This, of course, did not work because gopherjs does not try to interpret the dot notation. Now that I understand better I realize that the correct implementation is:

x := js.Global.Get("joint").Get("dia").Get("Graph").New()

A note regarding the "translation" of the dot notation would benefit many people. Thanks for your posts!