Challenge 1
Create a weak reference to an object.
const target = {id: 1};
const ref = new WeakRef(target);
console.log(ref.deref()?.id);
Press Ctrl⌘Enter to check your answer
Practice WeakRef, FinalizationRegistry, unregister tokens, and safe weak-lifetime handling.
Challenge 1
const target = {id: 1};
const ref = new WeakRef(target);
console.log(ref.deref()?.id);
Press Ctrl⌘Enter to check your answer
Challenge 2
const ref = new WeakRef({value: 9});
const value = ref.();
console.log(value?.value ?? "recompute");
Press Ctrl⌘Enter to check your answer
Challenge 3
const registry = new (held) => console.log(held));
console.log(registry instanceof FinalizationRegistry);
Press Ctrl⌘Enter to check your answer
Challenge 4
const registry = new FinalizationRegistry(() => {});
const target = {};
registry.(target, "metadata");
console.log("registered");
Press Ctrl⌘Enter to check your answer
Challenge 5
const registry = new FinalizationRegistry(() => {});
const target = {};
const token = {};
registry.register(target, "metadata", );
console.log("registered");
Press Ctrl⌘Enter to check your answer
Challenge 6
const registry = new FinalizationRegistry(() => {});
const target = {};
const token = {};
registry.register(target, "metadata", token);
console.log(registry.(token));
Press Ctrl⌘Enter to check your answer
Challenge 7
const symbol = Symbol("local");
const ref = new WeakRef(symbol);
console.log(ref.() === symbol);
Press Ctrl⌘Enter to check your answer
Challenge 8
function read(ref) {
const value = ref.deref();
return value ?? ();
}
const ref = new WeakRef({value: 1});
console.log(read(ref));
Press Ctrl⌘Enter to check your answer