mirror of
https://github.com/vercel-labs/scriptc.git
synced 2026-10-27 06:43:19 +08:00
A class constructor's `return expr` was evaluated and dropped, so `new C()` answered the constructed instance even where Node answers the returned object (`if (C.instance) return C.instance;` built a second instance). A return that yields an instance of the class itself stores it in a per-class result slot, read and cleared by one helper at every construction site (direct new, class values, the checked dynamic construction dispatchers, record-to-class coercion). A return of any other object refuses with SC1090 (compile time in TypeScript, a deferred fence in JavaScript); a JavaScript value typed only at run time is tested when it returns. super() into a class that can return an existing instance refuses, since Node would rebind the subclass's this. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
11 lines
295 B
TypeScript
11 lines
295 B
TypeScript
// Node's `new Point()` answers the object a constructor returns. A plain
|
|
// object has no representation as a Point instance, so the return refuses
|
|
// instead of answering the constructed instance.
|
|
class Point {
|
|
x = 1;
|
|
constructor() {
|
|
return { x: 2 };
|
|
}
|
|
}
|
|
console.log(new Point().x);
|