Skip to content

Instantly share code, notes, and snippets.

@ericprud
Last active June 9, 2020 12:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ericprud/ff7a7ab14140ba9b7604f659a90add08 to your computer and use it in GitHub Desktop.
Save ericprud/ff7a7ab14140ba9b7604f659a90add08 to your computer and use it in GitHub Desktop.
function vs. class constructor for a library
module.exports = class {
constructor (foo) {
class A {
constructor (a) {
this.a = foo(a);
}
}
class B {
constructor (b) {
this.b = foo(b);
}
}
return {A, B}
}
}
module.exports = class {
constructor (foo) {
class A {
constructor (a) {
this.a = foo(a);
}
}
class B {
constructor (b) {
this.b = foo(b);
}
}
this.ret = {A, B}
}
doIt () { return this.ret; }
}
module.exports = (foo) => {
class A {
constructor (a) {
this.a = foo(a);
}
}
class B {
constructor (b) {
this.b = foo(b);
}
}
return {A, B}
}
const Func = require('./initFunc')(MyFoo)
const Clas = new (require('./initClass'))(MyFoo)
console.warn('Func', new Func.A(1).a)
console.warn('Func', new Func.B('b').b)
console.warn('Clas', new Clas.A(2).a)
console.warn('Clas', new Clas.B('d').b)
function MyFoo (x) {
return x + x
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment