gpt4 book ai didi

JavaScript 变量 = 变量

转载 作者:行者123 更新时间:2023-11-30 12:43:56 27 4
gpt4 key购买 nike

在 JavaScript 中有没有办法让一个变量总是等于一个变量?喜欢var1 = var2但是当var2更新,也是var1 .

例子

var var1 = document.getElementById("demo").style.color;
var var2 = "blue";

var1 = var2;

var2 = "red";
//make var1 update to red when the statement above runs.

编辑:

我使用代码的实际上下文在 <progress> 中元素与 value属性,var2 将是数字而不是字符串。

最佳答案

在 javascript 中,如果要保留对值的引用,通常会使用对象。

var obj1 = {};
var obj2 = obj1;
// obj1 and obj2 both point at the same object
obj1.b = document.getElementById("demo").style.color;
obj2.b = "blue";

//both obj1.b and obj2.b are "blue" right now

关于JavaScript 变量 = 变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23305495/

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