use*_*129 9 javascript readline node.js console.readline
我正在学习node.js并需要readline用于项目.我直接从readline模块示例中获得以下代码.
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.question('What do you think of Node.js? ', (answer) => {
// TODO: Log the answer in a database
console.log('Thank you for your valuable feedback:', answer);
rl.close();
});
Run Code Online (Sandbox Code Playgroud)
但是当我通过node try.js命令运行代码时
,它会不断发出如下错误:
rl.question('What is your favorite food?', (answer) => {
^^
SyntaxError: Unexpected token =>
at exports.runInThisContext (vm.js:73:16)
at Module._compile (module.js:443:25)
at Object.Module._extensions..js (module.js:478:10)
at Module.load (module.js:355:32)
at Function.Module._load (module.js:310:12)
at Function.Module.runMain (module.js:501:10)
at startup (node.js:129:16)
at node.js:814:3
Run Code Online (Sandbox Code Playgroud)
Che*_*yDT 16
Arrow函数是ECMAScript 6标准的新功能之一,仅在4.0.0版本中引入node.js(作为稳定特性).
您可以升级node.js版本或使用旧语法,如下所示:
rl.question('What do you think of Node.js? ', function(answer) {
// TODO: Log the answer in a database
console.log('Thank you for your valuable feedback:', answer);
rl.close();
});
Run Code Online (Sandbox Code Playgroud)
(请注意,这些语法之间还有一个区别:this变量的行为方式不同.对于此示例而言,它无关紧要,但在其他示例中也可能无关紧要.)
| 归档时间: |
|
| 查看次数: |
11392 次 |
| 最近记录: |