-
Notifications
You must be signed in to change notification settings - Fork 83
Setter-only design vs. constructor arguments #8
Comments
Constructor arguments serve well instantiations like this: const Repeats = Superclass => class extends Superclass {
constructor(config) {
super();
this._container = config.container;
this._container.style.position = 'relative';
}
};
class MyList extends Repeats(class {});
const list = new MyList({
container: document.body,
// ...
}); But this won't work for custom elements because it forces them to have class MyList extends Repeats(HTMLElement) {
constructor() {
super({
container: this,
});
}
}
customElements.define('my-list', MyList); |
Uhm, I guess the custom element could just define getters for these properties and that's it, e.g. class MyList extends Repeats(HTMLElement) {
get _container() { return this; }
} |
As far as I know none of the classes currently in DESIGN.md are custom element classes, so I'm not sure why the concern applies... |
Yep, but I was working on updating to constructor arguments and realized that this doesn't work well for custom elements, e.g. the This would have to be changed to something like this to work with constructor arguments: class HTMLSpecViewer extends HTMLElement {
constructor() {
super();
this.style.display = 'block';
this.style.minHeight = '100000px';
const htmlSpec = new HtmlSpec();
htmlSpec.head.style.display = 'none';
this.appendChild(htmlSpec.head);
this._htmlSpec = htmlSpec;
this._list = new VirtualList({
items: [],
container: this,
layout: new Layout({
itemSize: {
y: 10000,
},
_overhang: 800,
}),
newChild: (item) => item,
recycleChild: () => {},
});
} |
Yeah, that makes a lot of sense to me. Prefer composition over inheritance, after all. |
https://github.com/valdrinkoshi/virtual-list/blob/design/DESIGN.md has a lot of examples where you construct various classes by creating empty versions of them and then setting a bunch of properties with
Object.assign()
. This is strange in a few ways:VirtualList
with nolayout
oritems
is still a fully-functionalVirtualList
.container
.)Instead I would suggest making these constructor arguments:
new VirtualList({ layout, container, items, newChildFn })
, etc. This ensures that you cannot construct an instance of the class without setting them, and setting them all at once, together.This also allows more fine-grained separation so that things which can be set and modified later and independently (say,
VirtualRepeater
'sfirst
?) are exposed as public getter/setter pairs.The text was updated successfully, but these errors were encountered: