mirror of
https://github.com/vercel-labs/scriptc.git
synced 2026-10-02 08:35:07 +08:00
- Preserve unbound class methods, call-time receivers, and bound callback arguments. - Keep native factory results, fallback functions, and mutable JavaScript renderer state intact. - Support live Error.cause assignment, deletion, and property descriptors across aliases.
28 lines
1.2 KiB
TypeScript
28 lines
1.2 KiB
TypeScript
class Counter {
|
|
value: number;
|
|
callback: (increment: number) => number;
|
|
constructor(value: number) { this.value = value; this.callback = this.add; }
|
|
add(increment: number): number { this.value += increment; return this.value; }
|
|
constant(): number { return 11; }
|
|
fail(): number { throw new Error("callback failed"); }
|
|
}
|
|
const first = new Counter(1);
|
|
const second = new Counter(10);
|
|
console.log(first.callback(2));
|
|
second.callback = first.callback;
|
|
console.log(second.callback(3), first.value);
|
|
console.log(first.callback === first.add, first.add === second.add);
|
|
const detached = first.callback;
|
|
try { detached(1); } catch (error) { if (error instanceof Error) console.log(error.name); }
|
|
const constant = first.constant;
|
|
console.log(constant());
|
|
const arrow = (value: number): number => value + 1;
|
|
const alias = arrow.bind(null);
|
|
const ordinary = (function(value: number): number { return value + 2; }).bind(null);
|
|
console.log(alias(4), ordinary(4));
|
|
const bound = first.add.bind(second);
|
|
console.log(bound(2), second.value, first.value);
|
|
console.log(first.add.call(second, 1), first.add.apply(first, [4]));
|
|
try { first.fail.bind(first)(); } catch (error) { if (error instanceof Error) console.log(error.message); }
|
|
console.log(bound(1));
|