New JavaScript Proposals Transform Error Handling, RegExp, and Binary Data
Several Stage‑3 TC39 proposals—including Error.isError for reliable error type checks, RegExp.escape for safe regex construction, Uint8Array methods for base64/hex conversion, and Temporal.TimeZone.prototype.equals for canonical time‑zone comparison—aim to enhance JavaScript’s core capabilities across browsers and Node.js.
When a proposal reaches Stage 3, browsers and Node.js begin implementing its features, and changes are rare unless major issues arise.
Error.isError
Proposal: proposal-is-error . It introduces a global method
Error.isErrorto accurately determine whether a value is an
Errorobject. The need arises because
Symbol.toStringTagcan make
Object.prototype.toString()return unreliable strings.
Using a fabricated
FakeErrorclass that returns
'Error'from its
[Symbol.toStringTag]getter, the method can correctly identify both real
Errorinstances and faked ones:
<code>class FakeError {
get [Symbol.toStringTag]() {
return 'Error';
}
}
// Output: [object Error]
console.log(Object.prototype.toString.call(new FakeError()));
// Output: [object Error]
console.log(Object.prototype.toString.call(new Error()));
// Output: true
console.log(Object.prototype.toString.call(new Error()) === Object.prototype.toString.call(new FakeError()));
</code>The popular
is-errorpackage implements the same check with
Object.prototype.toString() === '[object Error]'.
RegExp.escape
Proposal: proposal-regex-escaping . Developers often need to build regular expressions from strings without treating special characters as regex tokens, but no built‑in method exists.
Example of the problem:
<code>let text = "Hello.";
// this would match . against any character rather than matching it against a dot
ourLongText.replace(new RegExp(text, "g"));
</code>Escaping user input is also required for security, yet existing implementations may miss some characters or mishandle multi‑code‑unit UTF‑8 symbols (e.g.,
'😂'.length === 2).
The proposal adds
RegExp.escapeto escape all regex‑special characters:
<code>const str = prompt("Please enter a string");
const escaped = RegExp.escape(str);
const re = new RegExp(escaped, 'g'); // handles reg exp special tokens with the replacement.
console.log(ourLongText.replace(re));
</code>Examples:
<code>RegExp.escape("The Quick Brown Fox"); // "The Quick Brown Fox"
RegExp.escape("Buy it. use it. break it. fix it."); // "Buy it\. use it\. break it\. fix it\."
RegExp.escape("(*.*)"); // "\(\*\.\*\)"
RegExp.escape("。^・ェ・^。"); // "。\^・ェ・\^。"
RegExp.escape("😊 *_* +_+ ... 👍"); // "😊 \*_\* \+_\+ \.\.\. 👍"
RegExp.escape("\d \D (?:)"); // "\\d \\D \(\?:\)"
</code>Uint8Array to/from base64 and hex
Proposal: proposal-arraybuffer-base64 . It adds binary‑data conversion methods to
Uint8Array, enabling direct base64 and hexadecimal transformations.
Current JavaScript provides
btoaand
atobfor string‑based base64, but they are limited to Latin‑1 encoded data. The proposal introduces
Uint8Array.prototype.toBase64,
Uint8Array.fromBase64,
Uint8Array.prototype.toHex, and
Uint8Array.fromHexfor robust handling.
Base64 example:
<code>const binaryData = new Uint8Array([72, 101, 108, 108, 111, 44, 32, 87, 111, 114, 108, 100, 33]);
// binary data → Base64 encoding
const base64Data = btoa(String.fromCharCode.apply(null, binaryData));
console.log(base64Data); // "SGVsbG8sIFdvcmxkIQ=="
// Base64 encoding → binary data
const decodedBinaryData = new Uint8Array(atob(base64Data).split('').map(function(c) {
return c.charCodeAt(0);
}));
console.log(decodedBinaryData); // Uint8Array[ 72, 101, 108, 108, 111, 44, 32, 87, 111, 114, 108, 100, 33 ]
</code>Using the new methods:
<code>const raw = new Uint8Array([72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100]);
const str = raw.toBase64(); // 'SGVsbG8gV29ybGQ='
const arr = Uint8Array.fromBase64(str); // Uint8Array([72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100])
</code>Time Zone Canonicalization
Proposal: proposal-canonical-tz . Time‑zone names in ECMAScript come from the IANA Time Zone Database, which is periodically updated. The proposal standardizes handling of updated zones, simplifies identifiers, and adds
Temporal.TimeZone.prototype.equalsto compare two zones for equivalence.
<code>Temporal.TimeZone.from('Asia/Calcutta').equals('Asia/Kolkata'); // => true
</code>References
proposal-is-error: https://github.com/tc39/proposal-is-error
proposal-regex-escaping: https://github.com/tc39/proposal-regex-escaping
proposal-arraybuffer-base64: https://github.com/tc39/proposal-arraybuffer-base64#uint8array-tofrom-base64-and-hex
proposal-canonical-tz: https://github.com/tc39/proposal-canonical-tz
Time Zone Database: https://www.iana.org/time-zones
Taobao Frontend Technology
The frontend landscape is constantly evolving, with rapid innovations across familiar languages. Like us, your understanding of the frontend is continually refreshed. Join us on Taobao, a vibrant, all‑encompassing platform, to uncover limitless potential.
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.