『JavaScript』种草 ES2020 八大新功能( 三 )


  • 找不到模块的情况下也能处理故障
  • 运行方式:
    // Used as a function, import returns a promise that can be handled the 2 usuals ways:// Using callbackimport('/module.js').then((module) => {// Do something with the module.});// Using async / awaitasync function {let module = await import('/modules.js');} 下面是一些值得注意的区别 , 与通常的导入声明不同:
    • import 可在脚本中使用 , 而不仅是模块中;
    • import 可以在任何级别任何位置运行 , 而且不会被挂起;
    • import 可以接受任意字符串(需具有运行时确定的模板字符串 , 如下所示) , 而不仅是静态字符串文字 。

    ‘String.protype.matchAll’ matchAll 方法会检索字符串是否匹配给定的正则表达式(RegExp) , 并返回所有结果的迭代程序 , 以如下方式运行:
    const regexp = RegExp('[a-z]*ball','g');const str = 'basketball handball pingpong football';const matches = str.matchAll(regexp);// Since it is an iterator, you can loop trought results this way:let match = matches.next;while (!match.done) {console.log(match.value);match = matches.next;}// ["basketball", index: 0, input: "basketb...", groups: undefined]// ["handball", index: 11, input: "basketb...", groups: undefined]// ["football", index: 29, input: "basketb...", groups: undefined]// One can also get an array of all resultslet results = [...str.matchAll(regexp)]; 在与全局/g标志一同使用时 , match方法不返回捕获组 , 请使用matchAll 。
    以上是今年的全部新功能 。 Babel已经拥有上述几乎所有功能的插件 , 如果想要在项目中使用 , 可以参考如下:
    @babel/plugin-proposal-ish-coalescing-operator
    @babel/plugin-proposal-private-methods
    @babel/plugin-proposal-optional-chaining
    @babel/plugin-syntax-bigint
    @babel/plugin-syntax-dynamic-import
    原文链接:https://medium.com/better-programming/8-new-features-shipping-with-es2020-7a2721f710fb
    本文为 CSDN 翻译 , 转载请注明来源出处 。
    『JavaScript』种草 ES2020 八大新功能
    本文插图

    ?知识图谱够火 , 但底层技术环节还差点火候|AI技术生态论
    ?深度|一文读懂“情感计算”在零售中的应用发展
    ?三大运营商将上线5G消息;苹果谷歌联手 , 追踪30亿用户;jQuery 3.5.0发布|极客头条
    ?曾遭周鸿祎全网封杀的360猛将:草根打工到36岁身家上亿的逆袭!
    ?你公司的虚拟机还闲着?基于Jenkins和Kubernetes的持续集成测试实践了解一下!
    ?从Web1.0到Web3.0:详析这些年互联网的发展及未来方向
    ?一文读懂“情感计算”在零售中的应用发展


    推荐阅读