如何在Node.js 12中使用可选链

huangapple go评论164阅读模式
英文:

How to use optional chaining in Node.js 12

问题

Optional chaining (obj?.param1?.param2) 似乎是一个很棒的功能,我真的很想看到它实现并最终摆脱嵌套的 if 语句、任意的函数等等,用于如此简单的操作。

但是存在一个问题,它不起作用。我升级到 Node 12,仍然出现错误:

var dude = res?.param?.params[0]
SyntaxError: 意外的标记 '.'

或者

var dude = res.param?.params[0]
SyntaxError: 意外的标记 '.'

问题出在哪里?

我需要更改一些语言配置或者下载一个库来启用这个功能吗?还是它目前还没有发布?

英文:

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.

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 '.'

or

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

What is the problem?

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

答案1

得分: 155

Optional chaining 在 Node.js 版本 13 及以下目前不受支持。它将从 Node.js 版本 14 开始得到支持,并且大多数浏览器也将支持,因为它已移至 Stage 4。目前,只有少数平台支持它。您可以在提供的 链接 中找到支持 optional chaining 的平台列表。您可以使用 --harmony 标志启用 optional chaining。

英文:

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.

答案2

得分: 28

可选链特性的规范刚刚在第四阶段(已完成)上升级,日期为2019年12月22日。Node 12在规范最终确定之前发布,Node 13也是如此。

根据node.green,从Node 14开始将支持可选链,但仍需要使用--harmony标志。(这似乎与Node对--harmony标志的描述有冲突 - V8的特性不应该需要该标志 - 所以我不确定应该如何理解。)不管是否需要标志,我不会预期在大约2020年4月的Node 14发布之前看到这个特性。

如果您想今天就使用可选链,最好的选择是使用TypeScript(在版本3.7中添加了可选链)或类似Babel的预处理器。

英文:

The spec for the optional chaining feature was just promoted to Stage 4 (Finished) on December 22, 2019. Node 12 came out before the spec was final - and so did Node 13, for that matter.

According to node.green, optional chaining will be supported starting with Node 14, but will still require the --harmony flag. (This seems to conflict with Node's description of the --harmony flag - V8's shipping features aren't supposed to require the flag - so I'm not sure what to make of that.) Still, whether it needs a flag or not, I wouldn't expect to see the feature until the Node 14 release around April 2020.

If you want to play with optional chaining today, your best bet is to use TypeScript (which added optional chaining in version 3.7) or a preprocessor like Babel.

答案3

得分: 16

我能够使用带有 --harmony 标志的 nodejs v13.7.0。

node --harmony myCode.js

Dinah

undefined

undefined

// myCode.js

const adventurer = {
  name: 'Alice',
  cat: {
    name: 'Dinah'
  }
};

const catName = adventurer.cat?.name;
console.log(catName);
// 预期输出: Dinah
const dogName = adventurer.dog?.name;
console.log(dogName);
// 预期输出: undefined

console.log(adventurer.someNonExistentMethod?.())
// 预期输出: undefined
英文:

I was able to use nodejs v13.7.0 with --harmony flag.

> node --harmony myCode.js
>
> Dinah

> undefined

> undefined

//myCode.js

const adventurer = {
  name: 'Alice',
  cat: {
    name: 'Dinah'
  }
};

const catName = adventurer.cat?.name;
console.log(catName);
// expected output: Dinah
const dogName = adventurer.dog?.name;
console.log(dogName);
//expected output: undefined

console.log(adventurer.someNonExistentMethod?.())
//expected output: undefined

答案4

得分: 4

Optional Chaining将在Node.js v14中实现,该版本将于2020年04月20日发布。目前,您可以使用Babel和@babel/plugin-proposal-optional-chaining

英文:

Optional Chaining will be implemented with Node.js v14, which will be released on 20/04/2020. By now, you may use Babel with @babel/plugin-proposal-optional-chaining.

答案5

得分: 0

如果您仍然遇到此问题,请检查您正在使用的Node版本 node --version

如果您使用了 nvm,请确保您正在使用一个实现了导致错误的操作符的Node版本。

例如:

nvm install 15.8
nvm use 15.8
英文:

If you're still having this issue, check the node version you're using node --version.

If you have nvm, make sure you're using a node version that implements the operator that is giving an error.

e.g.

nvm install 15.8
nvm use 15.8

huangapple
  • 本文由 发表于 2020年1月3日 14:21:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/59574047.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定