New features coming in JavaScript 2018: asynchronous generators and better regular expressions
The latest annual ECMAScript update, released in June 2018, is still the largest annual release to date, although it is still much smaller than ECMAScript 6 in terms of the backlog of common features.
Brian Terlson, an ECMAScript editor and Microsoft’s representative on The ECMA TC39 committee, told The New Stack: Two of the biggest developer features in this release are asynchronous generators and some long-awaited regular expression improvements, as well as the REST/Spread attribute.
“Asynchronous generators and iterators are the result of combining asynchronous functions and iterators, so it’s like an asynchronous generator that you can wait in or an asynchronous function that you can get a return value from,” he explains. Previously, ECMAScript allowed you to write a function that could type or wait, but not both. “This is very convenient for an increasingly large portion of consumption streams on the Web platform, especially when Fetch objects expose streams.”
An asynchronous iterator is similar to the Observable pattern, but more flexible. “An Observable is a push model; Once you subscribe to it, you’re bombarded with events and notifications whether you’re ready or not, so you have to implement buffering or sampling strategies to deal with interruptions, “Terlson explained. Asynchronous iterators are push-pull models — you request a value and it’s sent to you — that work better for things like network IO primitives.
Promise. Prototype. Finally the asynchronous programming is also very helpful, in a state into a Promise fulfilled or rejected, specify a method to clean up eventually.
More general regular expression functionality
Terlson is particularly excited about the regular expression improvements (most of which have been done by the V8 team, who have already done early implementations of these four major features), because this is an area where the language has lagged.
“ECMAScript regular expressions have not improved significantly since the days of JavaScript; Almost all other programming languages have regular expression libraries that are more advanced than ECMAScript. ECMAScript 6 includes some minor updates, but he sees ECMAScript 2018 as “the first update that significantly changes how you write regular expressions.”
The dotAll flag makes the dot character match all characters, instead of matching some newlines (such as n or f). “You can’t use dots unless you’re in multi-line mode and you don’t care about the end of each line,” he points out. Workarounds in this area create regular expressions that are unnecessarily complex, and Terlson expects “everyone to use this pattern in regular expressions.”
Named capture groups are similar to named groups in many other languages, where you can take different parts of a string that a named regular expression matches and treat them as objects. “It’s almost the same as adding a comment to your regular expression that explains what the group is trying to capture by giving it a name,” he explains. “Part of the pattern is the month, this is the date of birth… It’s really helpful for other people to maintain your model in the future.”
There are other proposals for null characters, which tell the regular expression engine to ignore whitespace, line breaks, and comments in pattern matching and allow comments at the end of lines after whitespace, a feature that could be included in future versions of ECMAScript and further improve maintainability.
Previously, ECMAScript had antecedent assertions but no subsequent assertions. “People use tricks like flipping strings and then matching them, or some other hacks,” Terlson notes. This is especially useful for lookup and replace regular expressions. “What you see doesn’t become part of your match, so if you’re replacing numbers with dollar signs on either side, you can do that without having to do extra work to put the dollar signs back in.” ECMAScript postline assertions allow variable-length postline assertions like those in C#, not just the fixed-length pattern in Perl.
In particular, for developers who need to support international users, the Unicode attribute escape \\p{… is allowed in regular expressions. } and \ \ P {… } will make it easier to create Unicode-aware regular expressions. Right now, this is a hassle for developers.
“Unicode defines numbers, but these numbers include not only basic Latin ASCII 0 to 9, but also mathematical numbers, bold numbers, outline numbers, fancy presentation numbers, table numbers. To match any number in Unicode, a Unicode-recognized application must have the entire Unicode data table available. By adding this feature, you can delegate all of this to Unicode, “he said. If you want to match Unicode characters in a strict way, say for form validation, and you want to do the right thing rather than tell people their names are invalid, this is difficult in many cases, but with Unicode character classes you can specify the range of characters required for names. There are already classes for different languages and scripts, so if you just want to deal with Greek or Chinese, you can do that. Emoji are becoming more and more common.
There are also new internationalization apis for localized date and time formats, euro currency formats, and plural formats that make it easier to perform localized tabs and buttons.
ECMAScript 2018 extends support for objects and arrays for REST and Spread patterns (so common in the React ecosystem that many developers don’t realize it’s not fully standardized) that Terlson calls it a small feature with an outsized impact. Rest and Spread are useful for copying and cloning objects, for example, if you have an immutable structure and you want to change everything but one property, or if you want to copy an object but add an extra property. Terlson points out that this pattern is often used to assign default values to option records. “It’s a very good grammar model for what you do all the time.”
Converters such as Babel and TypeScript already support many features of ECMAScript 2018. Browser support will also come over time, and all new features are already in the release of Chrome (see the ECMAScript Compatibility Table for a complete matrix of support).
Browser support detected by the ECMAScript compatibility table.
(a larger version: https://cdn.thenewstack.io/media/2018/08/cf694974-ecmascript.png)
Future development; ECMAScript 2019
Some interesting proposals have yet to reach the stage 4 required to become part of the ECMAScript standard, including the slightly controversial idea of declaring private fields and methods, including many alternative proposals.
When classes were introduced in ECMAScript 6, they were “minimal,” which Terlson explains is “intentionally small [scoped] because we’ll continue to deal with them later.” Private fields allow developers to declare fields that can be referenced by name inside a class, but not accessible from outside the class, “he said. Not only does it provide better performance, since the runtime can better optimize object handling when declaring all fields in a class constructor, but the language enforces privacy in a way that private fields in TypeScript do not. Unlike Symbols, you can list all symbols on an object using the get property, and private fields will not allow reflection.
“Library authors are looking for a way to have private status so developers can’t rely on it,” Terlson explained. “Libraries don’t like to interrupt users even when they’re doing things they shouldn’t be doing.” For example, private attributes in a class will allow library authors to avoid exposing internal implementation details if they may change them in the future.
The BigInt proposal is also in phase three. Currently, ECMAScript only has 64-bit floating-point types, but many platforms and Web apis use 64-bit integers — including the 64-bit integers used by Twitter as tweet ids. “You can no longer represent tweet ids as numbers in JavaScript,” Terlson explained. “They must be represented as a string.” BigInt is a more general proposal for adding integers of arbitrary precision, not just 64-bit integers. Encryption apis and high-precision timers will also take advantage of this, and Terlson expects JIT JavaScript engines will likely use native 64-bit fields to provide large integers to improve performance.
Two proposals have entered the fourth phase; Make catch binding optional (you don’t have to pass variables to catch blocks if you don’t actually need to use them), and make small syntax changes to handle mismatches between JSON and ECMAScript string formats. These will enter ECMAScript 2019 along with other proposals that will make progress in the coming months.
Microsoft is a sponsor of The New Stack.
The first image is from Pixabay.
The original address: https://thenewstack.io/whats-coming-up-in-javascript-2018-async-generators-better-regex/
Mary Branscombe
The Nuggets translation Project
Permanent links to this article: https://github.com/xitu/gold-miner/blob/master/TODO1/whats-coming-up-in-javascript-2018-async-generators-better-regex.md
Translator: MeFelixWang
Proofreader: CoderMing