-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathES13.js
More file actions
57 lines (46 loc) · 1.33 KB
/
ES13.js
File metadata and controls
57 lines (46 loc) · 1.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// 1. Top-level await
// Note: This requires a module context to run properly.
const fetchData = async () => {
const response = await fetch('https://jsonplaceholder.typicode.com/posts/1');
const data = await response.json();
return data;
};
// Example usage
fetchData().then(data => console.log(data)); // Fetches and logs data from the API
// 2. Class Fields
class Person {
name = 'John Doe'; // Public field
#age = 30; // Private field
getAge() {
return this.#age;
}
}
// Example usage
const person = new Person();
console.log(person.name); // John Doe
console.log(person.getAge()); // 30
// 3. Private Methods
class Counter {
#count = 0;
#increment() {
this.#count++;
}
incrementAndGet() {
this.#increment();
return this.#count;
}
}
// Example usage
const counter = new Counter();
console.log(counter.incrementAndGet()); // 1
console.log(counter.incrementAndGet()); // 2
// 4. WeakRefs and FinalizationRegistry
const registry = new FinalizationRegistry((heldValue) => {
console.log(`Cleaning up: ${heldValue}`);
});
let obj = { name: 'Weak Reference Example' };
const weakRef = new WeakRef(obj);
// Example usage
registry.register(obj, 'Object 1');
obj = null; // Allow garbage collection
// The cleanup callback will be called when the object is garbage collected