gpt4 book ai didi

javascript - 如何更改 readline 函数中的变量?

转载 作者:行者123 更新时间:2023-11-30 12:10:16 25 4
gpt4 key购买 nike

我有以下代码,但我不知道如何更改函数内的变量 x。

var readline = require('readline');
var r1 = readline.createInterface({
input: process.stdin,
terminal: false
});
var x = 1;
r1.on('line', function(inputText) {
x = 2; // i want to be able to change it here
}
console.log(x); //prints 1 and I want 2

最佳答案

变量会改变 - 这只是你的函数是异步 - 所以你在 on('line') 函数之外的日志在变量赋值之前执行。

关于javascript - 如何更改 readline 函数中的变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34003642/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com