mirror of
https://github.com/freeCodeCamp/freeCodeCamp.git
synced 2026-05-28 18:26:54 +00:00
fix(curriculum): correct WeakSet example with valid references (#66981)
Co-authored-by: Jeevankumar S <jeevenkumar2003@gmail.com>
This commit is contained in:
+16
-12
@@ -218,13 +218,18 @@ const treeWeakSet = new WeakSet();
|
||||
```javascript
|
||||
const treeWeakSet = new WeakSet();
|
||||
|
||||
treeWeakSet.add({ name: 'Baobab' });
|
||||
treeWeakSet.add({ name: 'Jackalberry' });
|
||||
treeWeakSet.add({ name: 'Mopane Tree' });
|
||||
treeWeakSet.add({ name: 'Breadfruit' });
|
||||
const baobab = { name: "Baobab" };
|
||||
const jackalberry = { name: "Jackalberry" };
|
||||
const mopaneTree = { name: "Mopane Tree" };
|
||||
const breadfruit = { name: "Breadfruit" };
|
||||
|
||||
treeWeakSet.delete('Jackalberry');
|
||||
console.log(treeWeakSet.has('Jackalberry')); // false
|
||||
treeWeakSet.add(baobab);
|
||||
treeWeakSet.add(jackalberry);
|
||||
treeWeakSet.add(mopaneTree);
|
||||
treeWeakSet.add(breadfruit);
|
||||
|
||||
treeWeakSet.delete(jackalberry);
|
||||
console.log(treeWeakSet.has(jackalberry)); // false
|
||||
|
||||
console.log(treeWeakSet);
|
||||
```
|
||||
@@ -233,17 +238,16 @@ In the output, the contents of the `WeakSet` appear like this:
|
||||
|
||||
```javascript
|
||||
/*
|
||||
WeakSet {{…}, {…}, {…}, {…}}
|
||||
WeakSet {{…}, {…}, {…}}
|
||||
[[Entries]]
|
||||
No properties
|
||||
0: value: {name: 'Mopane Tree'}
|
||||
1: value: {name: 'Baobab'}
|
||||
2: value: {name: 'Breadfruit'}
|
||||
[[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:
|
||||
|
||||
|
||||
Reference in New Issue
Block a user