mirror of
https://github.com/vercel-labs/scriptc.git
synced 2026-10-28 06:53:42 +08:00
A static method that a function-local JavaScript class defines at
runtime and a program class inherits (Animal.extend("Dog")) is now
called directly with this bound to the receiver class when it captures
nothing from the factory's scope, so the subclass it builds has a static
base and constructs like Node.
Fixes #938
Co-authored-by: phall <25888173+phall1@users.noreply.github.com>
26 lines
566 B
JavaScript
26 lines
566 B
JavaScript
// A class factory whose classes extend themselves through a static
|
|
// method: `extend` builds a subclass of whichever class it is called on.
|
|
function factory(base, name) {
|
|
const out = class extends base {
|
|
constructor(value) {
|
|
super(value);
|
|
this.kind = name;
|
|
}
|
|
static extend(name2) {
|
|
return factory(this, name2);
|
|
}
|
|
describe() {
|
|
return `${this.kind}:${this.value}`;
|
|
}
|
|
};
|
|
return out;
|
|
}
|
|
class Root {
|
|
constructor(value) {
|
|
this.value = value;
|
|
}
|
|
}
|
|
export function Class(name) {
|
|
return factory(Root, name);
|
|
}
|