Understanding the Latest TC39 Proposals: Intl.Segmenter, Error Cause, RegExp Enhancements and More
This article reviews recent TC39 proposals that have reached Stage 4—including Intl.Segmenter, Error Cause, Intl.DurationFormat, Array.groupBy, Destructuring Private Fields, String.cooked, RegExp modifiers, RegExp \R, buffer boundaries, Evaluator Attributes, and Bind‑this—explaining their goals, usage, and providing practical JavaScript code examples.
TC39 Proposals Overview
The following sections summarize several ECMAScript proposals that have advanced to Stage 4, describing their purpose, key API changes, and example usage in JavaScript.
Intl.Segmenter
Provides native text segmentation based on Unicode UAX 29, enabling efficient word and sentence boundaries for languages such as Chinese without external dictionaries.
let segmenter = new Intl.Segmenter("zh-CN", { granularity: "word" });
let input = "我不是,我没有,你别瞎说。";
let segments = segmenter.segment(input);
for (let { segment, index, isWordLike } of segments) {
console.log(`segment at code units [${index}, ${index + segment.length}): «${segment}»${isWordLike ? " (word-like)" : ""}`);
}Error Cause
Adds a cause property to the Error constructor, allowing developers to chain errors and preserve original context across call stacks.
try {
return await fetch('//unintelligible-url-a')
.catch(err => { throw new Error('Download raw resource failed', { cause: err }); });
} catch (err) {
console.log(err);
console.log('Caused by', err.cause);
}Intl.DurationFormat
Introduces a standardized API for formatting time intervals, eliminating the need for heavy third‑party libraries.
let result = new Intl.DurationFormat("zh-CN", { style: "long" }).format({
hours: 1,
minutes: 46,
seconds: 40
});
// result => "1小时46分40秒"Array.groupBy
Provides a built‑in method to group array elements by a user‑defined key function, similar to Lodash's groupBy.
const array = [1, 2, 3, 4, 5];
const grouped = array.groupBy(i => (i % 2 === 0 ? 'even' : 'odd'));
// grouped => { odd: [1,3,5], even: [2,4] }Destructuring Private Fields
Allows private class fields (prefixed with #) to be used in destructuring assignments.
class Foo {
#x = 1;
constructor() {
console.log(this.#x); // 1
const { #x: x } = this; // x === 1
console.log(x);
}
}String.cooked
Introduces String.cooked, a counterpart to String.raw that processes escape sequences, useful for template‑string manipulation.
String.raw`Hi \u{597D}\u{5BB6}`; // "Hi \u{597D}\u{5BB6}"
String.cooked`Hi \u{597D}\u{5BB6}`; // "Hi 好家伙"RegExp Modifiers
Enables inline flag scopes so that modifiers such as i, m, s, and x can be turned on or off for specific sub‑expressions.
// Switch flags on/off
const re1 = /^(?i)[a-z](?-i)[a-z]$/;
re1.test('ab'); // true
re1.test('Ab'); // true
re1.test('aB'); // false
// Scoped flags inside a group
const re2 = /^(?i:[a-z](?-i:[a-z]))$/;
re2.test('ab'); // true
re2.test('Ab'); // true
re2.test('aB'); // falseRegExp \R Escape
Provides the \R escape to match any line‑break sequence, simplifying cross‑platform newline handling.
const lines = fs.readFileSync('file.txt', 'utf8').split(/\R/ug);RegExp Buffer Boundaries
Introduces \A and \z to match the absolute start and end of the input, and \Z for an optional final newline.
const re = /\Afoo\z/u; // matches only "foo"
const reEnd = /end\Z/u; // matches "end" or "end
"Evaluator Attributes (Import Reflection)
Adds an as clause to import statements, allowing the same module to be imported with different evaluator attributes (e.g., WebAssembly instance vs. module).
import FooModule from "./foo.wasm" as "wasm-module";
FooModule instanceof WebAssembly.Module; // trueBind‑this
Provides a concise syntax ( ::) to bind a function’s this to a target object, simplifying patterns like fn.call(obj, …).
const toSet = function () { return new Set(this); };
let classCount = document
::(allDivsAccessor.get)()
::flatMap(element => element.classList)
::toSet()
::(sizeDescriptor.get)();All of the above proposals aim to make JavaScript more expressive, performant, and easier to use across diverse environments.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
