Last active
June 9, 2020 12:15
-
-
Save ericprud/ff7a7ab14140ba9b7604f659a90add08 to your computer and use it in GitHub Desktop.
function vs. class constructor for a library
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| module.exports = class { | |
| constructor (foo) { | |
| class A { | |
| constructor (a) { | |
| this.a = foo(a); | |
| } | |
| } | |
| class B { | |
| constructor (b) { | |
| this.b = foo(b); | |
| } | |
| } | |
| return {A, B} | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| module.exports = (foo) => { | |
| class A { | |
| constructor (a) { | |
| this.a = foo(a); | |
| } | |
| } | |
| class B { | |
| constructor (b) { | |
| this.b = foo(b); | |
| } | |
| } | |
| return {A, B} | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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