Message from JavaScript discussions

April 2019

— But each selector could fail


const assertExists = (
parent = document.body,
) => {
const elem = parent
if (!elem) {
if (fallback) {
return assertExists(fallback, parent);
throw new Error(
'Selector matched no elements: ' +
return elem;

constructor(params) {
const { selector1, selector2 } = params; = assertExists(selector1); = assertExists(selector2,;

— What do you want to do if they fail?

— Thanks!
Depends on the elem. Some are necessary some are not. I want to be able to provide a fallback or if its a necessary elem at best delete the instance. Is it possible to delete a class instance inside of the class?

Message permanent page

— Throw during construction

— Prevents creating the class

— Oh nice. Perfect.

— What if the fallback fails?

— Also delete the instance / prevent creating one

— Edited

— Third argument is fallback selector

— Thanks. When assertExists is a class method and I call this method inside of the constructor and it throws. Does this also prevent creating the instance?

Message permanent page