如何在Node.js 12中使用可选链接 [英] How to use optional chaining in Node.js 12

查看:734
本文介绍了如何在Node.js 12中使用可选链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可选链接(obj?.param1?.param2)似乎是一个很棒的功能,我真的很想看到它的实现,并最终摆脱了嵌套的if,任意函数以及如此简单的操作所不具备的功能.

Optional chaining (obj?.param1?.param2) seems to be a great feature and I really wanted to see it implemented and finally get rid of nested ifs, arbitrary functions and what not for such a simple operation.

但是有一个问题,它不起作用.我已更新到节点12,但仍然出现错误:

But there's a problem, it doesn't work. I updated to Node 12 and I still get an error:

var dude = res?.param?.params[0]
SyntaxError: Unexpected token '.'

var dude = res.param?.params[0]
SyntaxError: Unexpected token '.'

出什么问题了?

是否需要更改某些语言配置或下载库才能启用此功能?还是根本还没有出来?

Do I need to change some language config or download a library to enable this feature? Or is it simply not out yet?

推荐答案

Node.js 13及更低版本当前不支持可选链接. Node.js版本14和大多数浏览器(移至第4阶段)都将支持它.目前,很少有平台支持它.您可以在给定的中找到支持可选链接的平台列表.链接.您可以使用--harmony标志启用可选功能.

Optional chaining is currently not supported in Node.js version 13 and below. It will be supported from Node.js version 14 and most of the browsers as it is moved to Stage 4. Currently, few platforms are supporting it. You can find the list of platforms supporting optional chaining in the given link. You can enable optional using --harmony flag.

这篇关于如何在Node.js 12中使用可选链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆