node.js - Readline操作的Node JS代码说明
问题描述
我是 Node JS 的新手,我写了下面的代码只是为了了解一些事情。
// for readline
**const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
var result = 0, num1, num2;
rl.question('Enter the first number ', (num1) =>{
rl.question('Enter the second number ', (num2) =>{
rl.question('Enter the operation ', (opr) => {
if(opr === '+'){result = +num1 + +num2; console.log(`Sum of ${num1} and ${num2} is ${result}`);}
else {console.log('Invalid Arithmatic Operation Specified')};
rl.close();
});
});
});
console.log(`${num1}`);
rl.on("close", function() {
console.log("\nBYE BYE !!!");
process.exit(0);
});**
这个简单的算术运算代码。根据此代码,最终控制台日志行 (console.log( ${num1}
);) 应在运行操作代码之后执行。但是,当我运行代码时,它会立即执行(请参阅下面的未定义输出)。有人可以解释为什么吗?
D:\nodejs\test>node maths.js
Enter the first number undefined
1
Enter the second number 2
Enter the operation +
Sum of 1 and 2 is 3
BYE BYE !!!
Regards,
John
解决方案
推荐阅读
- python - 如果您不知道日期输入的格式,如何格式化日期字符串?
- eclipse - 调试 (Eclipse):没有时间调试:浏览器超时并显示“内部服务器错误”
- spring-boot - 使用 javax 发送电子邮件的 Spring Boot REST 代码在我构建为 jar 时有效,但在我构建为战争时无效
- visual-studio-code - 如何重新启用vs代码中的所有扩展
- c - 在函数中使用 const 修饰符
- oracle - SQL 数据库触发器 ORA-00904
- c - codechef 中的约束
- php - PHP Monolog 作为 Logger 仅打印信息级别
- python - 如何使用 sympy 求解这个方程?
- github - Travis CI 上的 Composer 更新错误