[英]How to assign a Javascript variable to the same value of another variable
How do you make a Javascript value have the same value as another variable. 如何使Javascript值与另一个变量具有相同的值。 I have a variable called "buttonId", and another called "toWin", I want to make it so that buttonId's value doesn't change, but, toWin changes to have the same value as buttonId.
我有一个名为“buttonId”的变量,另一个名为“toWin”,我想使它成为buttonId的值不会改变,但是,toWin更改为具有与buttonId相同的值。 something like:
就像是:
toWin=buttonId;
Can anyone help me? 谁能帮我? No jQuery, please.
请不要jQuery。
Just like you have it, but if you're declaring it for the first time, use the var keyword. 就像你拥有它一样,但如果你是第一次声明它,请使用var关键字。
var toWin = buttonId;
If buttonId
is an object, you may want to clone the object before setting toWin
to the value of buttonId
to avoid changing the value of the original object ( buttonId
) when and if you change toWin
. 如果
buttonId
是一个对象,你可能需要设置之前,克隆对象toWin
,它的值buttonId
避免更改原始对象的(价值buttonId
)时,如果你改变toWin
。
You forgot to mention var while declaring the second variable. 你忘了在声明第二个变量时提到var。 you should do it like this:
你应该这样做:
var something = '1';
var anotherthing = something;
alert(anotherthing);
The output will be 1. 输出将为1。
This works: 这有效:
toWin=buttonId;
I was debugging my Javascript, and thought that this might be the problem, so I just checked on here if it works, but now I know that my problem is completely different. 我正在调试我的Javascript,并认为这可能是问题所以我只是在这里检查它是否有效,但现在我知道我的问题完全不同了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.