fix(curriculum): correct WeakSet example with valid references (#66981)

Co-authored-by: Jeevankumar S <jeevenkumar2003@gmail.com>
This commit is contained in:
yoehae
2026-04-24 18:44:22 +02:00
committed by GitHub
parent 504b37caf1
commit c8de54970e
@@ -218,13 +218,18 @@ const treeWeakSet = new WeakSet();
```javascript ```javascript
const treeWeakSet = new WeakSet(); const treeWeakSet = new WeakSet();
treeWeakSet.add({ name: 'Baobab' }); const baobab = { name: "Baobab" };
treeWeakSet.add({ name: 'Jackalberry' }); const jackalberry = { name: "Jackalberry" };
treeWeakSet.add({ name: 'Mopane Tree' }); const mopaneTree = { name: "Mopane Tree" };
treeWeakSet.add({ name: 'Breadfruit' }); const breadfruit = { name: "Breadfruit" };
treeWeakSet.delete('Jackalberry'); treeWeakSet.add(baobab);
console.log(treeWeakSet.has('Jackalberry')); // false treeWeakSet.add(jackalberry);
treeWeakSet.add(mopaneTree);
treeWeakSet.add(breadfruit);
treeWeakSet.delete(jackalberry);
console.log(treeWeakSet.has(jackalberry)); // false
console.log(treeWeakSet); console.log(treeWeakSet);
``` ```
@@ -233,17 +238,16 @@ In the output, the contents of the `WeakSet` appear like this:
```javascript ```javascript
/* /*
WeakSet {{…}, {…}, {…}, {…}} WeakSet {{…}, {…}, {…}}
[[Entries]] [[Entries]]
No properties 0: value: {name: 'Mopane Tree'}
1: value: {name: 'Baobab'}
2: value: {name: 'Breadfruit'}
[[Prototype]]: WeakSet [[Prototype]]: WeakSet
.
.
.
*/ */
``` ```
The contents appear empty because WeakSets are not iterable and do not expose their contents directly. The contents are visible here because the object variables still hold strong references, keeping those objects in memory. Keep in mind that WeakSets are not iterable, so there is no way to loop over their entries or access them programmatically. What you see in the output is only a debugging aid.
Don't forget that only symbols and objects with well-defined keys and values are supported. Adding a primitive, such as numbers or strings, will result in an error: Don't forget that only symbols and objects with well-defined keys and values are supported. Adding a primitive, such as numbers or strings, will result in an error: