节点 js:process.stdin.on 并使用 typeof 与 isNaN 测试数字
Node js : process.stdin.on and testing a number with typeof vs isNaN
我正在学习网络开发并开始我的旅程node.js(已经在 JS 上工作),如果我的问题是菜鸟的问题,我很抱歉 :) 我已经在论坛中找到了一些类似的问题但是不完全一样,还是不明白是怎么回事。
我不知道为什么 typeof 似乎在 node.js 中不起作用,因为它在控制台中使用 JS。我设法让它与 isNaN 函数一起工作,但是有人可以向我解释 typeof 函数发生了什么吗?
process.stdin.resume()
process.stdin.setEncoding('utf8')
console.log('What\'s your age ? ')
process.stdin.on('data', (int) => {
if (int > 99 ) {
console.log('Enter an age below 99')
} else if (typeof int != 'number') {
console.log('Enter a number')
console.log(int)
console.log(typeof int)
} else {
let year = 2020 - int
if (year > 2020) {
console.log("Year is above 2020")
} else {
console.log('Hello you were born in ' + year)
}
}
process.exit()
})
尝试使用 39 岁时,它会记录 'Enter a number',即使它是一个数字条目...! Console.log (typeof int) 给我 'string'.
如果我用 isNan 函数更改代码,它会完美运行:
process.stdin.resume()
process.stdin.setEncoding('utf8')
console.log('What\'s your age ? ')
process.stdin.on('data', (int) => {
if (int > 99 ) {
console.log('Enter an age below 99')
} else if (isNaN(int)) {
console.log('Enter a number')
console.log(int)
console.log(typeof int)
} else {
let year = 2020 - int
if (year > 2020) {
console.log("Year is above 2020")
} else {
console.log('Hello you were born in ' + year)
}
}
process.exit()
})
是否与process.stdin.on语句有关?
非常感谢您的回答!
typeof
不起作用,因为 process.stdin.on("data", callback)
的回调将在您调用 readable.setEncoding("utf8")
后收到一个字符串。
By default, no encoding is assigned and stream data will be returned
as Buffer objects. Setting an encoding causes the stream data to be
returned as strings of the specified encoding rather than as Buffer
objects. For instance, calling readable.setEncoding('utf8') will cause
the output data to be interpreted as UTF-8 data, and passed as
strings. Calling readable.setEncoding('hex') will cause the data to be
encoded in hexadecimal string format.
此外,typeof aStringValue === "number"
是 false
。
const foo = "foo";
console.log(typeof foo === "number"); // false
isNaN
将导致字符串转换为数字,这就是您的第二个示例起作用的原因。
无论如何,我认为处理这一切的最简单方法是使用 parseInt(int, 10)
和 try/catch 将数据显式转换为数字,而不是使用如此多的隐式转换。
我正在学习网络开发并开始我的旅程node.js(已经在 JS 上工作),如果我的问题是菜鸟的问题,我很抱歉 :) 我已经在论坛中找到了一些类似的问题但是不完全一样,还是不明白是怎么回事。
我不知道为什么 typeof 似乎在 node.js 中不起作用,因为它在控制台中使用 JS。我设法让它与 isNaN 函数一起工作,但是有人可以向我解释 typeof 函数发生了什么吗?
process.stdin.resume()
process.stdin.setEncoding('utf8')
console.log('What\'s your age ? ')
process.stdin.on('data', (int) => {
if (int > 99 ) {
console.log('Enter an age below 99')
} else if (typeof int != 'number') {
console.log('Enter a number')
console.log(int)
console.log(typeof int)
} else {
let year = 2020 - int
if (year > 2020) {
console.log("Year is above 2020")
} else {
console.log('Hello you were born in ' + year)
}
}
process.exit()
})
尝试使用 39 岁时,它会记录 'Enter a number',即使它是一个数字条目...! Console.log (typeof int) 给我 'string'.
如果我用 isNan 函数更改代码,它会完美运行:
process.stdin.resume()
process.stdin.setEncoding('utf8')
console.log('What\'s your age ? ')
process.stdin.on('data', (int) => {
if (int > 99 ) {
console.log('Enter an age below 99')
} else if (isNaN(int)) {
console.log('Enter a number')
console.log(int)
console.log(typeof int)
} else {
let year = 2020 - int
if (year > 2020) {
console.log("Year is above 2020")
} else {
console.log('Hello you were born in ' + year)
}
}
process.exit()
})
是否与process.stdin.on语句有关? 非常感谢您的回答!
typeof
不起作用,因为 process.stdin.on("data", callback)
的回调将在您调用 readable.setEncoding("utf8")
后收到一个字符串。
By default, no encoding is assigned and stream data will be returned as Buffer objects. Setting an encoding causes the stream data to be returned as strings of the specified encoding rather than as Buffer objects. For instance, calling readable.setEncoding('utf8') will cause the output data to be interpreted as UTF-8 data, and passed as strings. Calling readable.setEncoding('hex') will cause the data to be encoded in hexadecimal string format.
此外,typeof aStringValue === "number"
是 false
。
const foo = "foo";
console.log(typeof foo === "number"); // false
isNaN
将导致字符串转换为数字,这就是您的第二个示例起作用的原因。
无论如何,我认为处理这一切的最简单方法是使用 parseInt(int, 10)
和 try/catch 将数据显式转换为数字,而不是使用如此多的隐式转换。