10 Must-Have Web APIs for Senior Web Engineers! π
Some APIs in JavaScript may have a relatively low usage rate. Below we introduce their usage and usage scenarios one by one. π
Blob API π§¬
The Blob API is used to process binary data, and can easily convert data to or read data from Blob objects.
// Create a Blob object
const myBlob = new Blob(["Hello, world!"], { type: "text/plain" });
// Read the data of the Blob object
const reader = new FileReader();
reader.addEventListener("loadend", () => {
console.log(reader.result);
});
reader.readAsText(myBlob);Usage scenario: In web applications, binary files may need to be uploaded or downloaded, and these data can be conveniently processed using the Blob API. π€π₯
WeakSet π‘
A WeakSet is similar to a Set, but can store weakly referenced objects. This means that if there are no other references pointing to an object, then the object can be reclaimed by the garbage collector without having to be manually removed from the WeakSet.
// Note that these vars should be let to be reassignable
let obj1 = {};
let obj2 = {};
const myWeakSet = new WeakSet();
myWeakSet.add(obj1);
myWeakSet.add(obj2);
console.log(myWeakSet.has(obj1)); // true
obj1 = null; // obj1 can be garbage collected at some point in the future
console.log(myWeakSet.has(obj1)); // falseUsage scenario: WeakSets are useful when you want to create a collection of objects without preventing garbage collection. ποΈπ
TextEncoder and TextDecoder π
TextEncoder and TextDecoder are used to handle the conversion between strings and byte sequences. TextEncoder encodes strings into a UTF-8 array, and TextDecoder decodes UTF-8 arrays into strings.
const encoder = new TextEncoder();
const decoder = new TextDecoder('utf-8');
const view = encoder.encode('Hello, world!');
console.log(view); // Uint8Array(13) [72, 101, 108, 108, 111, 44, 32, 119, 111, 114, 108, 100, 33]
const decodedString = decoder.decode(view);
console.log(decodedString); // "Hello, world!"Usage scenario: These are especially useful for dealing with I/O data in web applications β things like streaming uploads or downloads, and communication with APIs that send data in binary format. ππ»
Proxy API ποΈ
const myObject = {
name: "John",
age: 30,
};
const myProxy = new Proxy(myObject, {
get(target, property) {
console.log(`Getting property ${property}`);
return target[property];
},
set(target, property, value) {
console.log(`Setting property ${property} to ${value}`);
target[property] = value;
return true;
},
});
console.log(myProxy.name); // "John"
myProxy.age = 31;Usage scenario: Proxy API is useful for intercepting and customizing operations on objects, such as property lookups, assignment, enumeration, function invocation, etc. π΅οΈββοΈπ§
Object.entries() and Object.values() π
const myObject = {
name: "John",
age: 30,
};
console.log(Object.entries(myObject)); // [["name", "John"], ["age", 30]]
console.log(Object.values(myObject)); // ["John", 30]Usage scenario: These methods are great for iterating over object properties when you need an array of keys or values. They enable functional programming patterns and transformations. πΆββοΈπ
IntersectionObserver ποΈ
const observer = new IntersectionObserver((entries, observer) => {
entries.forEach(entry => {
if (entry.isIntersecting) {
console.log(entry.target.id + " is visible");
observer.unobserve(entry.target);
}
});
});Usage scenario: IntersectionObserver is an excellent way to handle scenarios like lazy loading images or triggering animations when the user scrolls elements into view. π΅οΈπ‘
Symbol π
const mySymbol = Symbol("my unique symbol");
const myObject = {
[mySymbol]: "value",
myProperty: "value"
};
console.log(myObject[mySymbol]); // "value"
console.log(myObject.myProperty); // "value"Usage scenario: Symbols are useful for adding unique property keys to objects that wonβt clash with any other properties and can be used for private properties. ππ‘οΈ
Reflect API π
class MyClass {
constructor(value) {
this.value = value;
}
}
const instance = Reflect.construct(MyClass, ["myValue"]);
console.log(instance.value); // "myValue"Usage scenario: Reflect API provides methods for interceptable JavaScript operations. Itβs especially useful in meta-programming. π§ββοΈβ¨
Generator API π
function* idGenerator() {
let id = 0;
while(true) {
yield id++;
}
}
const myIdGenerator = idGenerator();
console.log(myIdGenerator.next().value); // 0
console.log(myIdGenerator.next().value); // 1Usage scenario: Generators are useful for lazy iterators, where the result is computed on-demand. This can be useful for infinite sequences, managing stateful iterations, and handling asynchronous processes. π€β±οΈ
Web Workers ποΈββοΈ
const worker = new Worker('worker.js');
worker.postMessage('Hello, worker');
worker.onmessage = function(e) {
console.log('Message from worker:', e.data);
};Usage scenario: Web Workers allow you to run JavaScript in background threads. This is useful for performing expensive calculations or handling high-latency operations without blocking the UI thread. πͺπ₯οΈ
AudioContext πΆ
const audioContext = new AudioContext();Usage scenario: AudioContext is crucial for web-based audio applications, allowing developers to manipulate audio for games, music apps, or interactive sound experiences. πποΈ
In summary
While some of these Web APIs may not be widely known, they offer powerful capabilities that can be leveraged to enhance user experiences and accommodate more complex web application requirements. Each serves a distinct purpose and can be used to solve specific problems you may encounter during development. π οΈπ
Stackademic
Thank you for reading until the end. Before you go:





