I feel like I should know this but how do I port sth like this to ClojureScript?
Do you need for it to also be importable from JS? In other words, do those export also have to be implemented in CLJS?
If no, does any of it have to be used from JS? Or is it CLJS-usage only?
don't need the export
more just not quite getting a hang of the prototype / this stuff
Treat prototype as a plain property, because it essentially is.
(set! (.-prototype Step)
#js {:areaStart (fn []
(this-as this
(set! (.-_line this) 0)))
...})If you use shadow-cljs, you can also require [shadow.cljs.modern :refer [defclass]] and then
(defclass Step
(constructor [context t] ...)
(areaStart [this] ...)
...)(I think it will result in functionally the same code. But it will be compiled to a class in JS.)
(defclass Step
(field _context)
(field _t)
(field _line js/NaN)
(constructor [this context t]
(set! _context context)
(set! _t t))
Object
(areaStart [this]
(set! _line 0))
(areaEnd [this]
(set! _line js/NaN))
...)yeah, functionality is the same. defclass is only available in shadow-cljs though.
sweet, I think that'll work for me!
The prototype stuff I sort of understood but I guess it wasn't entirely clear to me how to inject the initial variables into the function. This worked:
This sort of inspired a blogpost: https://martinklepsch.org/posts/embracing-js-files-in-clojurescript/
> “Leaf nodes” only, meaning you can’t require CLJS from JS and things like that. (Fine for my use cases.)
import { foo } from "goog:a.cljs.namespace"; gives you a.cljs.namespace/foo
danger with this is that there is no externs inference done for JS files, so if you do interop with "foreign" code it this it might require manual externs
otherwise this is exactly what I wrote this feature for. nice post.
I half assumed you’d know a way to require both directions 😀
haven't used it in a long time, so I hope its not broken 😛