3

Five JavaScript features that (almost) no one needs

 2 years ago
source link: https://devm.io/javascript/five-javascript-features
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Hitchhiking through the JavaScript jungle

Five JavaScript Features That (Almost) No One Needs

12. Sep 2022


I’m sure you know them: articles titled things like “The 10 best, newest, and most important JavaScript features”. Especially when it comes to a new version of the language standard, these articles spring up like mushrooms. But who cares about the language features that nobody actually wants? They were included in the standard full of good intentions and since then, they’ve lived in the shadows next to classes and Arrow functions in the JavaScript paradise. So let's take some time and look at five JavaScript features that nobody really needs.

Of course, we could pounce on the old familiar Bad Parts of JavaScript, which Douglas Crockford documented many years ago in his book “JavaScript: The Good Parts” and that his infamous JSLint so mercilessly denounces. The most famous representatives of this category are the with statement and the arguments pseudoarray. However, I’d like to deliberately keep a large distance from these language features. These are indeed the language’s dark side, which shouldn’t be joked about either as they cause a lot of headaches and bugs that are difficult to understand. JavaScript’s creators are doing their best to remove these inglorious features from the now almost 900-page standard document. But it’s not that simple because removing things from the web is practically unthinkable. There are even people who build their applications on these features. So at least do me a favor and stay far away from these un-features. Now that we've cleared that up, what are we going to look at? We’re talking about rather unknown language features that rarely find their way into applications, but do exist and have a right to exist. Let's start with template strings.

Template Strings

What? Template strings? That’s a feature that pretty much every JavaScript developer knows and uses at least ten times a day. Yes, of course that's true. Multiline strings and variable substitutions are incredibly handy. There are even linter rules, the prefer-template rule, that charmingly tell us to use template strings instead of string concatenation. So why should template strings suddenly be a feature that no one actually needs or wants? It’s not so much about template strings in general, but about a special feature of template strings, namely the tagging function. With this feature, you can implement your own handling of template strings. The browser’s standard function allows a simple replacement of variables:

function tag(strings, value) {
  return `${strings[0]}${value.toUpperCase()}${strings[1]}`;
}
const str = tag`Hello ${'javaScript'}!`;

console.log(str); // Output: Hello JAVASCRIPT!

You can implement these tagging functions yourself and in them, you have access to an array of strings that represents the individual static parts of the template string. All other arguments are placeholders. You can work with this information and do whatever you want. You don't even have to return a string. In reality, what can you use tagging functions for? You can implement your own template engine with them, for example. Or you can implement a library that creates new, good-looking components from React components, elements, and style definitions. Unfortunately, someone already had this idea before we did and gave it the melodious name Styled-Components. This library is actually a very nice example of using tagging features.

Property Descriptors

In JavaScript, you can do quite a few things with an object’s properties. To do this, you can use the Object.defineProperty method. It accepts an object reference, the name of the property to be defined, and an object with the property descriptors. You can see the different possibilities in Table 1. You can also easily see this feature in action in the example shown in Listing 1.

Descriptor Standard Meaning
value undefined The value of the property
writeable false Determines whether the value can be changed by an assignment
enumerable false Whether the property appears in iterations, e.g. in a for...in loop
configurable false Determines whether the type of the property can be changed and the property can be deleted
get undefined Function to read out the property...

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK