This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
I guess that proposal hasn't happened? https://clojure.atlassian.net/browse/CLJS-3084
Hasn't happened yet, yes.
However, if you're using shadow-cljs then you can use shadow.cljs.modern/defclass
.
I am but have since learned that for a typescript "implements" I don't actually need a subclass and in that case deftype probably works fine
just in case https://clojureverse.org/t/modern-js-with-cljs-class-and-template-literals/7450
what's the recommended way to translate something like this to cljs? it's consumed by a JS library so it has to work the same way. would deftype still work?
import { ParagraphNode } from "lexical";
export class CustomParagraphNode extends ParagraphNode {
static getType() {
return "custom-paragraph";
}
static clone(node) {
return new CustomParagraphNode(node.__key);
}
createDOM(config) {
const dom = super.createDOM(config);
dom.style = "background: green";
return dom;
}
}
context: https://codesandbox.io/p/sandbox/ecstatic-maxwell-kw5utu?file=%2Fsrc%2Fnodes%2FCustomParagraphNode.js%3A10%2C4I tried to follow this example: https://stackoverflow.com/questions/61891349/implementing-static-properties-in-clojurescript but got lost in the details (e.g. not sure how to translate super.createDOM(config)
). i also looked at shadow.cljs.modern/defclass
but it doesn't seem to support static methods
static methods can just be added outside the defclass, they are nothing special. the issue is that super isn't supported currently https://github.com/thheller/shadow-cljs/issues/1137
Same as writing CustomParagraphNode.clone = function(node) { ... };
in JS, not much different to just static
How would that work if something extends CustomParagraphNode
and tries to call clone
on the extending class?
I mean if they're given class X extends CustomParagraphNode
and then something calls X.clone
.
But I just tested - seems to be working fine:
class A {}
A.f = () => { console.log('f'); }
class B extends A {}
B.f()
=> f
I'm no exposed to JS that much, so seeing something being called from an inherited class but without prototype
feels wrong. :)
the only different is that this
is in the scope of that function, which you could achieve via bind
if needed
Thanks for this, and it looks like there's a workaround for super
too, although it emits an inference warning. Is there a good way to suppress the warning?
actually i take that back. as far as i understand there's no workaround for calling the super
constructor. am i stuck implementing this in JS?
shadow.cljs.modern/defclass
does support super
, I'm pretty sure, but specifically only for constructurs.
yeah no clue how to get the super
equivalent, kinda hard to search for. It is probably possible, I just haven't found it
@U2FRKM4TW i see! does it support multiple arity?
you don't want to use it in the constructor, at least the example above doesn't. so what he said doesn't matter
the example doesn't define a constructor, but looks like i need to define a constructor using defclass
, otherwise i get
AssertionError: Assert failed: contructor requires at least one argument name for this
the example calls the constructor with an argument:
CustomParagraphNode(node.__key);
which makes me think it's inheriting a multiple-arity constructor from its parent class (TBH I'm pretty fuzzy on JS class mechanics)I guess I could make constructor
optional. I just never had the case where I didn't want a constructor myself
no worries it was more a JS confusion. i got tripped up because there were other callsites where the same example constructor is called with no arguments. your answer helped me realize that it's legal in JS and it's just undefined
.
also for posterity I needed to add nvmd:compiler-options {:output-feature-set :es6}
to shadow-cljs.edn
otherwise i got all sorts of errors
ah, i must have made other changes. i got it from this article https://clojureverse.org/t/modern-js-with-cljs-class-and-template-literals/7450 but it's outdated
I was watching https://youtu.be/w5CCZQBNFSc?feature=shared&t=1453
and I am blown away by the usage of $0
in the video (I have linked to the exact moment of the video) to gain access to an obj
could anyone explain what does $0
meaning, like something like *1
in the repl?
> Published on Friday, March 22, 2019 • Updated on Wednesday, April 22, 2015 Heh, bloody time travelers.
this site is also really great https://devtoolstips.org/tips/en/get-recently-selected-dom-nodes-in-console/
what's the recommended way to translate something like this to cljs? it's consumed by a JS library so it has to work the same way. would deftype still work?
import { ParagraphNode } from "lexical";
export class CustomParagraphNode extends ParagraphNode {
static getType() {
return "custom-paragraph";
}
static clone(node) {
return new CustomParagraphNode(node.__key);
}
createDOM(config) {
const dom = super.createDOM(config);
dom.style = "background: green";
return dom;
}
}
context: https://codesandbox.io/p/sandbox/ecstatic-maxwell-kw5utu?file=%2Fsrc%2Fnodes%2FCustomParagraphNode.js%3A10%2C4