Created
January 12, 2023 15:26
-
-
Save jsumners/ed8597a20363c395e45b011bb22ac4f1 to your computer and use it in GitHub Desktop.
A weird construct in a class static method that makes the method inheritable by class extensions
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
'use strict' | |
class Foo { | |
static createFoo() { | |
return new Foo() | |
} | |
} | |
class Bar { | |
static createBar() { | |
return new (this)() | |
} | |
} | |
class Baz extends Foo {} | |
class FooBar extends Bar {} | |
const foo = Foo.createFoo() | |
console.log(foo instanceof Foo) | |
const bar = Bar.createBar() | |
console.log(bar instanceof Bar) | |
const baz1 = new Baz() | |
console.log(baz1 instanceof Baz) | |
console.log(baz1 instanceof Foo) | |
const baz2 = Baz.createFoo() | |
console.log(baz2 instanceof Baz) | |
console.log(baz2 instanceof Foo) | |
const foobar = FooBar.createBar() | |
console.log(foobar instanceof FooBar) | |
console.log(foobar instanceof Bar) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment