avatarXiuer Old

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

4582

Abstract

h APIs that send data in binary format. πŸ”„πŸ’»</p><h2 id="9aba">Proxy API πŸ–‡οΈ</h2><div id="ac79"><pre><span class="hljs-keyword">const</span> myObject = { <span class="hljs-attr">name</span>: <span class="hljs-string">"John"</span>, <span class="hljs-attr">age</span>: <span class="hljs-number">30</span>, }; <span class="hljs-keyword">const</span> myProxy = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Proxy</span>(myObject, { <span class="hljs-title function_">get</span>(<span class="hljs-params">target, property</span>) { <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">Getting property <span class="hljs-subst">${property}</span></span>); <span class="hljs-keyword">return</span> target[property]; }, <span class="hljs-title function_">set</span>(<span class="hljs-params">target, property, value</span>) { <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">Setting property <span class="hljs-subst">${property}</span> to <span class="hljs-subst">${value}</span></span>); target[property] = value; <span class="hljs-keyword">return</span> <span class="hljs-literal">true</span>; }, }); <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myProxy.<span class="hljs-property">name</span>); <span class="hljs-comment">// "John"</span> myProxy.<span class="hljs-property">age</span> = <span class="hljs-number">31</span>;</pre></div><p id="a940">Usage scenario: Proxy API is useful for intercepting and customizing operations on objects, such as property lookups, assignment, enumeration, function invocation, etc. πŸ•΅οΈβ€β™‚οΈπŸ”§</p><h2 id="2d6d">Object.entries() and Object.values() πŸ”</h2><div id="1d55"><pre>const myObject = { name: <span class="hljs-string">"John"</span>, age: <span class="hljs-number">30</span>, }; console.<span class="hljs-built_in">log</span>(Object.entries(myObject)); // <span class="hljs-string">[["name", "John"], ["age", 30]]</span> console.<span class="hljs-built_in">log</span>(Object.values(myObject)); // [<span class="hljs-string">"John"</span>, <span class="hljs-number">30</span>]</pre></div><p id="03af">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. πŸšΆβ€β™‚οΈπŸ“Š</p><h2 id="d14f">IntersectionObserver πŸ‘οΈ</h2><div id="d740"><pre><span class="hljs-keyword">const</span> observer = <span class="hljs-keyword">new</span> <span class="hljs-title class_">IntersectionObserver</span>(<span class="hljs-function">(<span class="hljs-params">entries, observer</span>) =></span> { entries.<span class="hljs-title function_">forEach</span>(<span class="hljs-function"><span class="hljs-params">entry</span> =></span> { <span class="hljs-keyword">if</span> (entry.<span class="hljs-property">isIntersecting</span>) { <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(entry.<span class="hljs-property">target</span>.<span class="hljs-property">id</span> + <span class="hljs-string">" is visible"</span>); observer.<span class="hljs-title function_">unobserve</span>(entry.<span class="hljs-property">target</span>); } }); });</pre></div><p id="4e0c">Usage scenario: IntersectionObserver is an excellent way to handle scenarios like lazy loading images or triggering animations when the user scrolls elements into view. πŸ•΅οΈπŸ’‘</p><h2 id="75f7">Symbol πŸ”</h2><div id="2ae5"><pre><span class="hljs-keyword">const</span> mySymbol = <span class="hljs-title class_">Symbol</span>(<span class="hljs-string">"my unique symbol"</span>); <span class="hljs-keyword">const</span> myObject = { [mySymbol]: <span class="hljs-string">"value"</span>, <span class="hljs-attr">myProperty</span>: <span class="hljs-string">"value"</span> }; <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myObject[mySymbol]); <span class="hljs-comment">// "value"</span> <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myObject.<span class="hljs-property">myProperty</span>); <span class="hljs-comment">// "value"</span></pre></div><p id="9056">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. πŸ”‘πŸ›‘οΈ</p><

Options

h2 id="4cf7">Reflect API 🌟</h2><div id="83dc"><pre><span class="hljs-keyword">class</span> <span class="hljs-title class_">MyClass</span> { <span class="hljs-title function_">constructor</span>(<span class="hljs-params">value</span>) { <span class="hljs-variable language_">this</span>.<span class="hljs-property">value</span> = value; } } <span class="hljs-keyword">const</span> instance = <span class="hljs-title class_">Reflect</span>.<span class="hljs-title function_">construct</span>(<span class="hljs-title class_">MyClass</span>, [<span class="hljs-string">"myValue"</span>]); <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(instance.<span class="hljs-property">value</span>); <span class="hljs-comment">// "myValue"</span></pre></div><p id="3992">Usage scenario: Reflect API provides methods for interceptable JavaScript operations. It’s especially useful in meta-programming. πŸ§™β€β™‚οΈβœ¨</p><h2 id="64eb">Generator API πŸŒ€</h2><div id="14a8"><pre><span class="hljs-keyword">function</span>* <span class="hljs-title function_">idGenerator</span>(<span class="hljs-params"></span>) { <span class="hljs-keyword">let</span> id = <span class="hljs-number">0</span>; <span class="hljs-keyword">while</span>(<span class="hljs-literal">true</span>) { <span class="hljs-keyword">yield</span> id++; } }

<span class="hljs-keyword">const</span> myIdGenerator = <span class="hljs-title function_">idGenerator</span>(); <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myIdGenerator.<span class="hljs-title function_">next</span>().<span class="hljs-property">value</span>); <span class="hljs-comment">// 0</span> <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(myIdGenerator.<span class="hljs-title function_">next</span>().<span class="hljs-property">value</span>); <span class="hljs-comment">// 1</span></pre></div><p id="6931">Usage 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. πŸ’€β±οΈ</p><h2 id="3cc1">Web Workers πŸ‹οΈβ€β™‚οΈ</h2><div id="e08e"><pre><span class="hljs-keyword">const</span> worker = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Worker</span>(<span class="hljs-string">'worker.js'</span>); worker.<span class="hljs-title function_">postMessage</span>(<span class="hljs-string">'Hello, worker'</span>); worker.<span class="hljs-property">onmessage</span> = <span class="hljs-keyword">function</span>(<span class="hljs-params">e</span>) { <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">'Message from worker:'</span>, e.<span class="hljs-property">data</span>); };</pre></div><p id="68c9">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. πŸ’ͺπŸ–₯️</p><h2 id="416f">AudioContext 🎢</h2><div id="9f27"><pre><span class="hljs-type">const</span> audioContext = <span class="hljs-keyword">new</span> <span class="hljs-built_in">AudioContext</span>();</pre></div><p id="b3ad">Usage scenario: AudioContext is crucial for web-based audio applications, allowing developers to manipulate audio for games, music apps, or interactive sound experiences. πŸ”ŠπŸŽ›οΈ</p><h2 id="efd1">In summary</h2><p id="aa89">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. πŸ› οΈπŸš€</p><h1 id="ef1e">Stackademic</h1><p id="3db4">Thank you for reading until the end. Before you go:</p><ul><li>Please consider <b>clapping</b> and <b>following</b> the writer! πŸ‘</li><li>Follow us <a href="https://twitter.com/stackademichq"><b>X</b></a><b> | <a href="https://www.linkedin.com/company/stackademic">LinkedIn</a> | <a href="https://www.youtube.com/c/stackademic">YouTube</a> | <a href="https://discord.gg/in-plain-english-709094664682340443">Discord</a></b></li><li>Visit our other platforms: <a href="https://plainenglish.io"><b>In Plain English</b></a><b> | <a href="https://cofeed.app/">CoFeed</a> | <a href="https://venturemagazine.net/">Venture</a></b></li></ul></article></body>

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)); // false

Usage 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); // 1

Usage 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:

JavaScript
Technology
Web Development
Programming
Coding
Recommended from ReadMedium