简体   繁体   English

关于 JavaScript 中变量的快速菜鸟问题

[英]Quick noob question about variables in JavaScript

Stupid question, but if I have code like this:愚蠢的问题,但如果我有这样的代码:

var x;
var y;
var z = x + y;

And then through the program I update the variables for x and y, but when I use the console to check var z, it gives me NaN.然后通过程序我更新了 x 和 y 的变量,但是当我使用控制台检查 var z 时,它给了我 NaN。 So the program is doing that calculation once at the beginning and not updating Z as it continues.所以程序在开始时只进行一次计算,而不是在继续时更新 Z。 So what's the solution to keep checking Z for the variable changes?那么,不断检查 Z 的变量变化的解决方案是什么? Do I have to do a loop with setInterval to keep checking the new updated Z variable?我是否必须使用 setInterval 进行循环以继续检查新更新的 Z 变量? Thanks谢谢

Yeah, your comment is right.是的,你的评论是对的。 JavaScript doesn't provide a way to create a live binging. JavaScript 不提供创建实时 binging 的方法。

So what's the solution to keep checking Z for the variable changes?那么,不断检查 Z 的变量变化的解决方案是什么?

All depends on what you are trying to do.一切都取决于您要尝试做什么。

What you think about the code:你对代码的看法:

var x = 1;
var y = 2;
var z = x + y;
console.log("The value of z variable: ", z);

PS.附注。 Preferably you should use let :最好你应该使用let

let x = 1;
let y = 2;
let z = x + y;
console.log("The value of z variable: ", z);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM