How to Use New CSS “:Is()” for Easy Element Targeting

www.sitepoint.com sitepoint.com3 years ago in #Dev Love330

https://voltomedia.com/fresh/wp-content/uploads/Create-a-Toggle-Switch-in-React-as-a-Reusable-Component.png TypeScript allows us to decorate specification-compliant ECMAScript with type information that we can analyze and output as plain JavaScript using a dedicated compiler. In large-scale projects, this sort of static analysis can catch potential bugs ahead of resorting to lengthy debugging sessions, let alone deploying to production. However, reference types in TypeScript are still mutable, which can lead to unintended side effects in our software. In this article, we’ll look at possible constructs where prohibiting references from being mutated can be beneficial. Need a refresher on immutability in JavaScript? Read our guide, Immutability in JavaScript. Primitives vs Reference Types JavaScript defines two overarching groups of data types: Primitives: low-level values that are immutable (e.g. strings, numbers, booleans etc.) References: collections of properties, representing identifiable heap memory, that are mutable (e.g. objects, arrays, Map etc.) Say we declare a constant, to which we assign a string: const message = ‘hello’; Given that strings are primitives and are thus immutable, we’re unable to directly modify this value. It can only be used to produce new values: console.log(message.replace(‘h’, ‘sm’)); // ‘smello’ console.log(message); // ‘hello’ Despite invoking replace() upon message, we aren’t modifying its memory. We’re merely creating a new string, leaving the original contents of message intact. Mutating the indices of message is a no-op by default, but will throw a TypeError in strict mode: ‘use strict’; const message = ‘hello’; message[0] = ‘j’; // TypeError: 0 is read-only Note that if the declaration of message were to use the let keyword, we would be able to replace the value to which it resolves: let message = ‘hello’; message = ‘goodbye’; It’s important to highlight that this is not mutation. Instead, we’re replacing one immutable value with another. Mutable References Let’s contrast the behavior of primitives with references. Let’s declare an object with a couple of properties: const me = { name: ‘James’, age: 29, }; Given that JavaScript objects are mutable, we can change its existing properties and add new ones: me.name = ‘Rob’; me.isTall = true; console.log(me); // Object { name: “Rob”, age: 29, isTall: true }; Unlike primitives, objects can be directly mutated without being replaced by a new reference. We can prove this by sharing a single object across two declarations: const me = { name: ‘James’, age: 29, }; const rob = me; rob.name = ‘Rob’; console.log(me); // { name: ‘Rob’, age: 29 } JavaScript arrays, which inherit from Object.prototype, are also mutable: const names = [‘James’, ‘Sarah’, ‘Rob’]; names[2] = ‘Layla’; console.log(names); // Array(3) [ ‘James’, ‘Sarah’, ‘Layla’ ] What’s the Issue with Mutable References? Consider we have a mutable array of the first five Fibonacci numbers: const fibonacci = [1, 2, 3, 5, 8]; log2(fibonacci); // replaces each item, n, with Math.log2(n); appendFibonacci(fibonacci, 5, 5); // appends the next five Fibonacci numbers to the input array This code may seem innocuous on the surface, but since log2 mutates the array it receives, our fibonacci array will no longer exclusively represent Fibonacci numbers as the name would otherwise suggest. Instead, fibonacci would become [0, 1, 1.584962500721156, 2.321928094887362, 3, 13, 21, 34, 55, 89]. One could therefore argue that the names of these declarations are semantically inaccurate, making the flow of the program harder to follow. Pseudo-immutable Objects in JavaScript Although JavaScript objects are mutable, we can take advantage of particular constructs to deep clone references, namely spread syntax: const me = { name: ‘James’, age: 29, address: { house: ‘123’, street: ‘Fake Street’, town: ‘Fakesville’, country: ‘United States’, zip: 12345, }, }; const rob = { …me, …

Like to keep reading?

This article first appeared on sitepoint.com. If you'd like to keep reading, follow the white rabbit.

View Full Article

Leave a Reply