为什么可选链在我的 Node REPL 中不起作用? [英] Why is optional chaining not working in my Node REPL?

查看:47
本文介绍了为什么可选链在我的 Node REPL 中不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从MDN

中复制粘贴的示例代码 但可选链接在我的节点(v12.13.0)REPL 中不起作用.抛出一个语法错误,说问号后面的点无效.这是怎么回事 ?我已经在 React App 中使用了这个表达式,它似乎工作正常.

I literally copy pasted example code from MDN but optional chaining wont work in my node(v12.13.0) REPL. Throws out a syntax error saying that the dot after the question mark is invalid. What's going on ? I have already used this expression in a React App and it seems to be working fine.

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


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

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

推荐答案

它仅适用于 --harmony 下的 Node 14:如何在 Node.js 12 中使用可选链

It's only coming for Node 14 under --harmony: How to use optional chaining in Node.js 12

如果你想在 node 中使用这个功能,你可以使用 babel 可选链接插件:https://babeljs.io/docs/en/next/babel-plugin-syntax-optional-chaining.html

You can use babel optional chaining plugin if you want this feature in node: https://babeljs.io/docs/en/next/babel-plugin-syntax-optional-chaining.html

这篇关于为什么可选链在我的 Node REPL 中不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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