Message from JavaScript discussions

May 2019

— But ehh

— 

How is this better?

import { AssertExists } from "helper";

class Foo extends AssertExists {
constructor () {
}
}

— The correct way imo:

class Foo {
constructor(assertExists) {
this.assertExists = assertExists;
}
}

— Then you pass it down during initialization

— Along with other deps

— Optionally in an object:

class Foo {
constructor(deps) {
Object.assign(this, deps);
}
}


then in initialization:

new Foo({ assertExists });

Message permanent page

— But then I would create the function for every instance instead of having it on the class or?

— No?

— The function already exists

— It's just an extra instance prop

— Doesn't really matter since assertExists is mostly a pure function, but the ideal way is to dependency-inject everything

Message permanent page

— But what is the advantage of passing it as a prop instead of defining it on the class?