Created
December 19, 2014 21:58
-
-
Save jacobstr/fbca6104b5e83f22fe50 to your computer and use it in GitHub Desktop.
ES6 Static Factory Magic - How does it work?
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
| class Parent { | |
| static build() { | |
| return new this(); | |
| } | |
| } | |
| class Child extends Parent { | |
| } | |
| assert(Child.build() instanceof Child); |
Author
Author
Well that was a derp - copying and pasting an earlier test led to me calling:
new Child.build();
Instead of
Child.build();
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This pattern works in the browser for me via Reactify's transformer.
It fails during my tests where I'm using the 6to5 jest plugin.
It seems like this might be a bit of a grey area. I'm counting on the fact that
thisshould be (in lieu of call / apply / bind) bound to "nearest" object to which the function is attached. In this case, that should be Child.If you stick a
console.log(this)inside of build(), the browser prints Child, jest prints{}.Parent.build()works in both cases, however.