为什么可选链在我的 Node REPL 中不起作用? [英] Why is optional chaining not working in my Node REPL?
问题描述
我从MDN 中复制粘贴的示例代码
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屋!