最近,“ 可选链接”和默认属性值(“ 空位合并” )已移至TC39流程的最后第四步。
实际上,这意味着这些创新和其他创新将成为2020年JavaScript标准的一部分。 我们将在本文中考虑它们。
您可以在此处跟踪浏览器支持(“ 2020功能”)-大约。 佩雷夫
当需要在同一行上捕获多个组时,使用粘性或全局正则表达式可能并不容易。
String.prototype.match在存在全局标志的情况下不会返回捕获的组,没有它,您将只能获得模板及其组的第一个完全匹配项。
一个例子:
let regexp = /t(e)(st(\d?))/g; let str = 'test1test2'; const results = str.match(regexp); console.log(results);
结果与标志“ g”
没有标志“ g”的结果使用String.prototype.matchAll可确保返回所有匹配项及其组。
一个例子:
let regexp = /t(e)(st(\d?))/g; let str = 'test1test2'; let array = [...str.matchAll(regexp)]; console.log(array);
结果:
在BigInt之前,以Number表示的最大值为2׳-1(MAX_SAFE_INTEGER)。 JavaScript现在将具有可以超过限制的原语。
您可以通过在数字上添加'n'或使用BigInt()函数来创建BigInt 。
一个例子:
let bigInt = 4n console.log(bigInt * bigInt)
- BigInt不等于Number ,但是可以强制转换为后者。
- 当执行除法运算时,结果将四舍五入到最接近的整数。
- 如果没有类型转换,则不能与Number一起使用。
试图用数字添加BigInt ...
let bigInt = 4n + 2 console.log(bigInt)
...导致异常:
在JavaScript世界中访问全局对象一直是头疼的事。 您应该熟悉该环境的特定语法,这会在编写可移植代码时造成不必要的困难,并有必要使用诸如getGlobal之类的东西。
一个例子:
var getGlobal = function () { if (typeof self !== 'undefined') { return self; } if (typeof window !== 'undefined') { return window; } if (typeof global !== 'undefined') { return global; } throw new Error('no global object found'); }; var globals = getGlobal();
随着globalThis的出现,您可以停止考虑环境,并统一全局对象。
一个例子:
globalThis.someFunction = () => 'hello' console.log(globalThis.someFunction())
假设您有一些承诺,并且想要在完成之后做点什么(成功与否无关紧要)。 Promise.allSettled就是用于此目的。
一个例子:
const fulfilledPromise = Promise.resolve("success"); const rejectedPromise = Promise.reject("error") const promises = [fulfilledPromise, rejectedPromise]; Promise.allSettled(promises). then((results) => results.forEach((result) => console.log(result)));
结果:
是否要在运行时根据特定条件加载模块? 现在无需第三方库即可完成。
调用import函数就足够了,它将返回promise。
一个例子:
import("some_module") .then(module => { module.doSomething(); }) .catch(err => { console.error(err.message); });
与静态导入不同,在静态导入中,您需要显式指定模块名称,例如,在动态导入期间,您可以将模板传递给函数。
使用模板的示例:
import(`${some_module}.js`) .then(module => { module.doSomething(); }) .catch(err => { console.error(err.message); });
当需要获取属性或默认值时,如果它为null或undefined ,我们通常使用'||'运算符。
在Nullish合并之前:
const response = someResponse.properties.mayBeUndefined || 'Response';
但是,想象一下,属性具有“假”值。
使用'||'的问题:
const someResponse = {properties: { mayBeUndefined: ''}} const response = someResponse.properties.mayBeUndefined || 'Response'; console.log(response)
结果:
这是不受欢迎的行为。 在这种情况下,我们需要一个属性值,而不是默认值。
使用Nullish合并不会出现此问题。 仅对空或未定义的属性返回默认值。
使用空位合并:
const someResponse = {properties: { mayBeUndefined: ''}} const response = someResponse.properties.mayBeUndefined ?? 'Response'; console.log(response)
结果:
要访问子属性,我们需要确保上面的属性存在。 到目前为止,每个超属性的存在都需要手动检查。
可选链接之前:
const someObj = { property: 'prop', otherProperty: { name: 'prop2' } }; const property = someObj.NotOtherProperty ? someObj.NotOtherProperty.name: undefined; console.log(property);
随着Optional Chaining的出现,您可以使用'?。'运算符 用于访问子属性。 下面的代码与上面的代码等效。
使用可选链接:
const someObj = { property: 'prop', otherProperty: { name: 'prop2' } }; const property = someObj.NotOtherProperty?.name; console.log(property);
可选链接使代码更简洁,尤其是在字符串中包含很多属性的情况下。
结论
您可以尝试从控制台中的一篇文章中运行示例: 其中一些已经在最新版本的浏览器中实现,其他可能很快就会实现。 一种或另一种方式,已经有可能开始将本文中的可能性与2020年语言联系起来。